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" }]
3

1 Answer 1

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)

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.