我只是一个快速的初学者。我想过滤具有某些属性的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
}]
我想要一个marked
和correct
具有相同值的数组。
我怎么能在swift中做到这一点?
在使用对象将其转换为数组后,您可以简单地使用。
myArray.filter { $0.marked == $0.correct }
如果它只是字典,您可以尝试按键查找其值
myArray.filter { $0["marked"]! == $0["correct"]! }