根据过滤后的对象数组中的数据创建新的对象数组

时间:2019-07-30 18:26:21

标签: javascript arrays

我有一个对象数组,其键名为“ selected”,该键设置为false。我从原始列表中筛选出一组对象,其中“选择”键设置为true。如何创建一个新数组,其中基于过滤后的列表使用真实值更新原始列表?

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)

最终结果应该是

    [{
        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**
      }
    ]

2 个答案:

答案 0 :(得分:2)

filtered数组的选定颜色名称存储在Set中。然后使用“映射时设置”来决定选择哪些项目:

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)

答案 1 :(得分:2)

您需要检查color属性,如果找到对象,则返回一个具有更改的属性的新对象。

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; }