过滤具有特定属性Swift的json数组

问题描述 投票:0回答:1

我只是一个快速的初学者。我想过滤具有某些属性的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中做到这一点?

arrays json swift swifty-json
1个回答
3
投票

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

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

如果它只是字典,您可以尝试按键查找其值

myArray.filter { $0["marked"]! == $0["correct"]! }
© www.soinside.com 2019 - 2024. All rights reserved.