0

I am trying to get the items that are in "colors" but not in "colors2" and show them, but instead i'm always getting all the items of the first array

let colors = [
    { key: "green", value: '#00b894' },
    { key: "lgreen", value: '#64ed9f' },
    { key: "yellow", value: '#edc611' },
    { key: "orange", value: '#fda044' },
    { key: "red", value: '#e74c47' }
]

let colors2 = [
    { key: "Option 1", value: "#00b894" },
    { key: "Option 2", value: "#e74c74" }
]

comparer = (otherArray) =>{
   return function (current) {
      return otherArray.filter(function (other) {
         return other.value !== current.value
      })
   }
 }

As for the output I am getting all the items of colors array. Instead I want to display lgreen, yellow and orange whose values are not the same as items in colors2 array.

Output :

Array [Object { key: "green", value: "#00b894" }, Object { key: "lgreen", value: "#64ed9f" }, Object { key: "yellow", value: "#edc611" }, Object { key: "orange", value: "#fda044" }, Object { key: "red", value: "#e74c47" }]
Andrew
  • 57
  • 9
  • This might help https://stackoverflow.com/questions/21987909/how-to-get-the-difference-between-two-arrays-of-objects-in-javascript – Tom Shaw Jan 19 '20 at 20:19
  • I think the line `return function (current)` is not necessary here – Mikkel Jan 19 '20 at 20:24
  • Does this answer your question? [JavaScript get elements from an object array that are not in another](https://stackoverflow.com/questions/50836835/javascript-get-elements-from-an-object-array-that-are-not-in-another) – Muhammad Usman Jan 19 '20 at 20:26

1 Answers1

2

Use filter() and some()

let colors1=[{key:"green",value:"#00b894"},{key:"lgreen",value:"#64ed9f"},{key:"yellow",value:"#edc611"},{key:"orange",value:"#fda044"},{key:"red",value:"#e74c47"}],colors2=[{key:"Option 1",value:"#00b894"},{key:"Option 2",value:"#e74c74"}];

let result = colors1.filter(color1 => {
    return !colors2.some(color2 => color1.value === color2.value)
})

console.log(result)
symlink
  • 11,984
  • 7
  • 29
  • 50