1

I have an array of objects with a key called 'selected' which is set to false. I have a filtered array of objects from the original list where the key 'selected' is set to true. How do I create a new array where the original list is updated with true values based on the filtered list?

const data = [{
    color: "red",
    value: "#f00",
    selected: false
  },
  {
    color: "green",
    value: "#0f0",
    selected: false
  },
  {
    color: "blue",
    value: "#00f",
    selected: false
  },
  {
    color: "cyan",
    value: "#0ff",
    selected: false
  },
  {
    color: "magenta",
    value: "#f0f",
    selected: false
  },
  {
    color: "yellow",
    value: "#ff0",
    selected: false
  },
  {
    color: "black",
    value: "#000",
    selected: false
  }
]

const filtered = [{
  color: "magenta",
  value: "#f0f",
  selected: true
}, {
  color: "green",
  value: "#0f0",
  selected: true
}, {
  color: "black",
  value: "#000",
  selected: true
}]

const newData = data.map(item => {
  item.selected = filtered.find(item1 => item1.id === item.id).selected
  return item
})

console.log(newData)

The end result should be

    [{
        color: "red",
        value: "#f00",
        selected: false
      },
      {
        color: "green",
        value: "#0f0",
        selected: **true**
      },
      {
        color: "blue",
        value: "#00f",
        selected: false
      },
      {
        color: "cyan",
        value: "#0ff",
        selected: false
      },
      {
        color: "magenta",
        value: "#f0f",
        selected: **true**
      },
      {
        color: "yellow",
        value: "#ff0",
        selected: false
      },
      {
        color: "black",
        value: "#000",
        selected: **true**
      }
    ]
1
  • 2
    You are trying to find objects using the id property, which doesn't exists on your items. Try item1.color === item.color instead Commented Jul 30, 2019 at 18:30

2 Answers 2

2

You need to check color property and return a new object with a change of the property if an object is found.

const 
    data = [{ color: "red", value: "#f00", selected: false }, { color: "green", value: "#0f0", selected: false }, { color: "blue", value: "#00f", selected: false }, { color: "cyan", value: "#0ff", selected: false }, { color: "magenta", value: "#f0f", selected: false }, { color: "yellow", value: "#ff0", selected: false }, { color: "black", value: "#000", selected: false }],
    filtered = [{ color: "magenta", value: "#f0f", selected: true }, { color: "green", value: "#0f0", selected: true }, { color: "black", value: "#000", selected: true }],
    newData = data.map(item => {
        var object = filtered.find(item1 => item1.color === item.color);
        return Object.assign({}, item, object && { selected: object.selected });
    });

console.log(newData)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

3 Comments

Since all properties are the same, why not - ({ ...item, ...object })?
why not just object? it's quite unclear if a new object is required or an update (mutation) as seen in the question. in this case mapping is not required.
Yep. You're right.You can just return the filtered item.
2

Store the selected color names of the filtered array in a Set. Then use the Set when mapping to decide which items are selected:

const data = [{"color":"red","value":"#f00","selected":false},{"color":"green","value":"#0f0","selected":false},{"color":"blue","value":"#00f","selected":false},{"color":"cyan","value":"#0ff","selected":false},{"color":"magenta","value":"#f0f","selected":false},{"color":"yellow","value":"#ff0","selected":false},{"color":"black","value":"#000","selected":false}]
const filtered = [{"color":"magenta","value":"#f0f","selected":true},{"color":"green","value":"#0f0","selected":true},{"color":"black","value":"#000","selected":true}]

const selectedSet = new Set(filtered.map(o => o.color))

const newData = data.map(o => selectedSet.has(o.color) ? ({ ...o, selected: true }) : o)

console.log(newData)

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.