1

I have 2 different sets of data and so far this is what i have done

var data1 = [{
  name: "one",
  index: 1
}, {
  name: "two",
  index: 2
}, {
  name: "three",
  index: 3
}, {
  name: "four",
  index: 4
}, {
  name: "five",
  index: 5
}]

var data2 = [{
  name: "one",
  index: 1
}, {
  name: "two",
  index: 2
}]

var mappeddata2 = data2.map((item) => {
  return item.index
})


for (var a = 0; a < data2.length; a++) {
  console.log("console log", data1.filter((item) => {
    return item.index !== mappeddata2[a]
  }))
}

What I wanted is differentiate data1 from data2 and the result should be like this

[{name: "three", index: 3},{name: "four", index: 4},{name: "five", index: 5}]
Nimantha
  • 6,405
  • 6
  • 28
  • 69
kcNeko
  • 587
  • 1
  • 7
  • 23
  • 3
    And what have you tried doing? – UnholySheep Jun 07 '17 at 07:46
  • Possible Dup of [JavaScript array difference](//stackoverflow.com/q/1187518) – Tushar Jun 07 '17 at 07:46
  • Can you please show us an example of code you tried? – Andrea Jun 07 '17 at 07:46
  • Important thing to note here is that your two data sets, even though they have what appears to be same data, have different values so any simple comparison wont work. You need a specific method for these types of objects. – Dellirium Jun 07 '17 at 07:48
  • There's no built in intersect method. Either way you loop through both arrays and compare the values or (easier) you use JavaScript's Array.filter(). – Chris Jun 07 '17 at 07:49
  • Problem is, two values that appear same are actually not same, so you need a specific method for comparing these types of objects, with these properties – Dellirium Jun 07 '17 at 07:53

0 Answers0