JavaScript-按键减少对象数组

时间:2018-08-20 17:10:22

标签: javascript arrays javascript-objects

使用一系列对象,如:

const arr = [
  {name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
  {name: "survey with select", value: "survey with select answer", count: 2},
  {name: "werasd", value: "Donald", count: 1},
  {name: "werasd", value: "Jim", count: 1}
];

我正在尝试根据name键的匹配值来减少数组,并实现所需的输出,如:

desiredOutput = [
  {name: "qewregf dqewafs", data: [{value: "qewregf dqewafs answer", count: 2}]},
  {name: "survey with select", data: [{value: "survey with select answer", count: 2}]},
  {name: "werasd", data: [{value: "Donald", count: 1}, {value: "Jim", count: 1}]}
]

这种尝试减少了数组,但是我不知道如何在不覆盖的情况下合并嵌套值。

const arr = [{"name":"qewregf dqewafs","value":"qewregf dqewafs answer","count":2},{"name":"survey with select","value":"survey with select answer","count":2},{"name":"werasd","value":"Donald","count":1},{"name":"werasd","value":"Jim","count":1}];

const result = arr.reduce((acc, d) => {
  const found = acc.find(a => a.name === d.name);
  const value = { name: d.name, val: d.value };
  if (found) {
    acc.push(...value);
  }
  else {
    acc.push({ name: d.name, data: [{ value: d.value }, { count: d.count }] });
  }
  return acc;
}, []);

console.log(result);

我想念什么?

5 个答案:

答案 0 :(得分:3)

您的代码有点接近目标,只需要进行一些调整即可。

请在下面的演示中查看评论:

  1. acc.find找不到任何内容时,则推入一个元素{name:d.name, data: [value]}

  2. 如果找到,则将一个{value: ...}推入数据属性。

const arr = [
  {name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
  {name: "survey with select", value: "survey with select answer", count: 2},
  {name: "werasd", value: "Donald", count: 1},
  {name: "werasd", value: "Jim", count: 1}
];

const result = arr.reduce((acc, d) => {
  const found = acc.find(a => a.name === d.name);
  //const value = { name: d.name, val: d.value };
  const value = { value: d.value, count: d.count }; // the element in data property
  if (!found) {
    //acc.push(...value);
    acc.push({name:d.name, data: [value]}) // not found, so need to add data property
  }
  else {
    //acc.push({ name: d.name, data: [{ value: d.value }, { count: d.count }] });
    found.data.push(value) // if found, that means data property exists, so just push new element to found.data.
  }
  return acc;
}, []);

console.log(result)

答案 1 :(得分:0)

通过检查找到的对象是否存在,并将其推入结果集中,可以简化其使用。

然后将带有valuecount的对象推到data

const
    array = [{ name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2 }, { name: "survey with select", value: "survey with select answer", count: 2 }, { name: "werasd", value: "Donald", count: 1 }, { name: "werasd", value: "Jim", count: 1 }],
    result = array.reduce((r, { name, value, count }) => {
        var temp = r.find(o => name === o.name);
        if (!temp) {
            r.push(temp = { name, data: [] });
        }
        temp.data.push({ value, count });
        return r;
    }, []);

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

答案 2 :(得分:0)

您需要执行以下操作:

const arr = [
  {name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
  {name: "survey with select", value: "survey with select answer", count: 2},
  {name: "werasd", value: "Donald", count: 1},
  {name: "werasd", value: "Jim", count: 1}
];

const result = arr.reduce((acc, d) => {
  let idx = acc.findIndex(a => a.name === d.name);
  let val = { value: d.value, count: d.count };

  if (idx > -1) {
    acc[idx].data.push(val);
  } else {
    acc.push({ name: d.name, data: [val] });
  }

  return acc;
}, []);

console.log(result);

您必须先找到索引,然后将数据放入该行。您只是找到它,然后将新值推入数组。

答案 3 :(得分:0)

您并不遥远。这是对代码的两行进行简单的更改即可实现:

const arr = [
  {name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
  {name: "survey with select", value: "survey with select answer", count: 2},
  {name: "werasd", value: "Donald", count: 1},
  {name: "werasd", value: "Jim", count: 1}
];

const result = arr.reduce((acc, d) => {
  const found = acc.find(a => a.name === d.name);
  const value = { name: d.name, val: d.value };
  if (found) {
    found.data.push(value);
  }
  else {
    acc.push({ name: d.name, data: [{ value: d.value, count: d.count }] });
  }
  return acc;
}, []);

console.log(result)

以下是区别:

-     acc.push(...value);
+     found.data.push(value);


-    acc.push({ name: d.name, data: [{ value: d.value }, { count: d.count }] });
+    acc.push({ name: d.name, data: [{ value: d.value, count: d.count }] });

答案 4 :(得分:0)

只需创建一个以名称为键的地图,然后使用Object destructingObject.values()即可获得所需的结果。

const arr = [
  {name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2},
  {name: "survey with select", value: "survey with select answer", count: 2},
  {name: "werasd", value: "Donald", count: 1},
  {name: "werasd", value: "Jim", count: 1}
];

let result = Object.values(arr.reduce((a,{name, ...props})=>{
  if(!a[name])
     a[name]  = Object.assign({}, {name,data : [props]});
   else
    a[name].data.push(props);
  return a;
},{}));

console.log(result);