从JSON数组中的每个元素中删除一个键——Ruby

时间:2021-10-04 21:22:10

So, I have a JSON array through which I am looping, and adding three keys and their values, I also want to remove/delete a key(subject_id) from every element in the array.

因此,我有一个正在循环的JSON数组,并添加三个键及其值,我还想从数组中的每个元素中删除/删除一个键(subject_id)。

How to go about doing this?

该怎么做呢?

s=JSON.parse(subjects.to_json)
s.each do |j|
  j["total_classes"] = total_classes[i]
  j["attended_classes"] = attended_classes[i]
  j["subject_name"] = subject_name[i]
  j["subject_id"] #remove this key, how?
  i=i+1
end

render json: { attendances: s }

1 个解决方案

#1


4  

Try this one

试试这个

j.delete("subject_id")

#1


4  

Try this one

试试这个

j.delete("subject_id")