JSON的过滤器阵列具有某些属性Swift



我只是Swift的初学者。我想过滤具有某些属性的JSON对象,并从中创建新的JSON数组。

我的数组是:

[{
  "marked" : 4,
  "attempted" : true,
  "correct" : 4,
  "subject" : 1,
  "status" : true,
  "question" : 550,
  "answer" : 34256,
  "time" : 23,
  "score" : 10,
  "chapter" : 26
}, {
  "marked" : 1,
  "attempted" : true,
  "correct" : 1,
  "subject" : 1,
  "status" : true,
  "question" : 566,
  "answer" : 34317,
  "time" : 33,
  "score" : 14,
  "chapter" : 26
}, {
  "marked" : 4,
  "attempted" : true,
  "correct" : 1,
  "subject" : 1,
  "status" : true,
  "question" : 590,
  "answer" : 34276,
  "time" : 33,
  "score" : 15,
  "chapter" : 26
}]

我想要一个 markedcorrect的数组。

我该如何在Swift中做到这一点?

将其转换为用对象的数组后,您可以简单地使用。

myArray.filter { $0.marked == $0.correct }

如果只是字典,您可以尝试通过键

来寻求其价值
myArray.filter { $0["marked"]! == $0["correct"]! }

最新更新