0

我有 json 格式的散列数组,我必须从该数组中删除一个散列,我正在迭代该数组,如果该特定键/值匹配我正在删除该散列,我找到 clear() 方法但是,清除离开 {},我不需要我希望删除整个哈希

[{"question":"0a2a3452","answer":"lopq"},
 {"question":"58deacf9","answer":"admirationo"},
 {"question":"32c53e","answer":"acion"},
 {"question":"b5546bcf","answer":"figure"},
 {"question":"4f246a10","answer":"zelta"},
 {"question":"bf546c04","answer":"deltaa"}]

即如果我的密钥匹配为“0a2a3452”,我想删除第一个哈希

4

2 回答 2

2

您可以使用delete_if以下方法:

arr = [{"question":"0a2a3452","answer":"lopq"},
       {"question":"58deacf9","answer":"admirationo"},
       {"question":"32c53e","answer":"acion"},
       {"question":"b5546bcf","answer":"figure"},
       {"question":"4f246a10","answer":"zelta"},
       {"question":"bf546c04","answer":"deltaa"}]

arr.delete_if {|a| a[:question] == '0a2a3452' }
于 2018-02-16T06:36:27.810 回答
1

尝试这个:

items = [
  {"question":"0a2a3452","answer":"lopq"},
  {"question":"58deacf9","answer":"admirationo"},
  {"question":"32c53e","answer":"acion"},
  {"question":"b5546bcf","answer":"figure"},
  {"question":"4f246a10","answer":"zelta"},
  {"question":"bf546c04","answer":"deltaa"}
]

items = items.reject {|i| i[:question] == '0a2a3452'}
于 2018-02-16T06:36:03.430 回答