根据属性合并对象数组中的元素

时间:2020-03-06 12:43:26

标签: javascript arrays ecmascript-6

我有一个对象数组。每个对象都有一个数量和值属性。如果一个对象具有相同的金额值,我想将该值添加到该对象中。

这是一个示例数组:

Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)

我想将其转换为类似于以下内容:

const array = [
  {
    "key": 1,
    "amount": 11,
    "value": "were"
  },
  {
    "key": 2,
    "amount": 6,
    "value": "locomotives"
  },
  {
    "key": 3,
    "amount": 5,
    "value": "They"
  },
  {
    "key": 4,
    "amount": 5,
    "value": "with"
  },
  {
    "key": 5,
    "amount": 4,
    "value": "used"
  }
]

我尝试过const array = [ { "key": 1, "amount": 11, "value": "were" }, { "key": 2, "amount": 6, "value": "locomotives" }, { "key": 3, "amount": 5, "value": "They, width" }, { "key": 5, "amount": 4, "value": "used" } ] reduce,但似乎无法加入,

5 个答案:

答案 0 :(得分:1)

我认为应该与.reduce()一起使用:

const array = [
  {
    "key": 1,
    "amount": 11,
    "value": "were"
  },
  {
    "key": 2,
    "amount": 6,
    "value": "locomotives"
  },
  {
    "key": 3,
    "amount": 5,
    "value": "They"
  },
  {
    "key": 4,
    "amount": 5,
    "value": "with"
  },
  {
    "key": 5,
    "amount": 4,
    "value": "used"
  }
];

const result = array.reduce((a, c) => {
  const found = a.find(e => e.amount === c.amount);  
  if (found) found.value = `${found.value}, ${c.value}`;
  return found ? a : a.concat(c);
}, []);

console.log(result);

希望对您有帮助!

答案 1 :(得分:1)

通过将amount值编入索引,可以将.reduce()与ES6 Map一起使用。如果地图中已经存在对象的amount值,则可以更新其value以包括当前对象value。如果amount值不在地图中,则可以将其设置为键,将当前对象设置为该值。最后,您可以使用Array.from().values()返回的迭代器中获取对象值数组

const array = [ { "key": 1, "amount": 11, "value": "were" }, { "key": 2, "amount": 6, "value": "locomotives" }, { "key": 3, "amount": 5, "value": "They" }, { "key": 4, "amount": 5, "value": "with" }, { "key": 5, "amount": 4, "value": "used" } ];

const res = Array.from(array.reduce((m, o) => {
  const curr = m.get(o.amount);
  return m.set(o.amount, curr && {...curr, value: `${curr.value}, ${o.value}`} || o);
}, new Map).values());
console.log(res);

答案 2 :(得分:0)

我的..

const array1 = 
      [ { key: 1, amount: 11, value: "were"        } 
      , { key: 2, amount:  6, value: "locomotives" } 
      , { key: 3, amount:  5, value: "They"        } 
      , { key: 4, amount:  5, value: "with"        } 
      , { key: 5, amount:  4, value: "used"        } 
      ] 


const array2 = array1.reduce((a,c)=>
                {
                let same = a.find(e=>e.amount===c.amount)
                if (same) same.value += ', '+c.value
                else a.push(c)
                return a
                },[])

console.log( array2 )

答案 3 :(得分:0)

reduce方法的每次迭代中,如果已经添加了value,我们可以添加value

const result = array.reduce((a, c) => {
  a[c.amount] = a[c.amount] || c;
  if ((Object.keys(a).includes(c.amount.toString())) && (a[c.amount].value!= c.value))
      a[c.amount].value += ', ' + c.value;
  return a;
}, {});

一个例子:

const array = [
  {
    "key": 1,
    "amount": 11,
    "value": "were"
  },
  {
    "key": 2,
    "amount": 6,
    "value": "locomotives"
  },
  {
    "key": 3,
    "amount": 5,
    "value": "They"
  },
  {
    "key": 4,
    "amount": 5,
    "value": "with"
  },
  {
    "key": 5,
    "amount": 4,
    "value": "used"
  }
];

const result = array.reduce((a, c) => {
  a[c.amount] = a[c.amount] || c;
  if ((Object.keys(a).includes(c.amount.toString())) && (a[c.amount].value!= c.value))
      a[c.amount].value += ', ' + c.value;
  return a;
}, {});

console.log(result);

答案 4 :(得分:0)

使用forEach循环并构建一个对象。如果amount键已经存在,则附加值字符串。

const update = data => {
  const res = {};
  data.forEach(item => {
    res[item.amount] =
      item.amount in res
        ? {
            ...res[item.amount],
            value: `${res[item.amount].value}, ${item.value}`
          }
        : { ...item };
  });
  return Object.values(res);
};

const array = [
  {
    key: 1,
    amount: 11,
    value: "were"
  },
  {
    key: 2,
    amount: 6,
    value: "locomotives"
  },
  {
    key: 3,
    amount: 5,
    value: "They"
  },
  {
    key: 4,
    amount: 5,
    value: "with"
  },
  {
    key: 5,
    amount: 4,
    value: "used"
  }
];

console.log(update(array));