0

I am just a beginner in swift. i want to filter array of json objects having certain property and create new array of json from that.

my array is :

[{
  "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
}]

i want an array in which marked and correct have same values.

How can i do that in swift?

1 Answer 1

3

After you've converted this to array with objects, you can simply use.

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

If it's just dictionary, you can try to seek their values by key

myArray.filter { $0["marked"]! == $0["correct"]! }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.