-1

In the following javascript arrray of objects i want to check if every question id in array two found in array one

arrayOne=[{"question":"100","response":"aaaa"},
             {"question":"200","response":"aaaa"}]  

arrayTwo=[{"question":"100","output":true},
             {"question":"200","output":true}]  

examples

if arrayTwo

 arrayTwo=[{"question":"100","output":true}]

return false

if arrayTwo

    arrayTwo=[{"question":"100","output":true},
             {"question":"200","output":true}]  

return true.

How to make function to check the two arrays?

Ali-Alrabi
  • 1,515
  • 6
  • 27
  • 60

1 Answers1

1

You can use every() and find() to do this, and it will return true/false as result.

var arrayOne = [{
  "question": "100",
  "response": "aaaa"
}, {
  "question": "200",
  "response": "aaaa"
}]

var arrayTwo = [{
  "question": "100",
  "output": true
}, {
  "question": "200",
  "output": true
}]

var result = arrayTwo.every(function(e) {
  return arrayOne.find(function(a) {
    return a.question == e.question;
  })
})

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176