在匹配id javascript的基础上计算元素

时间:2017-07-08 16:18:18

标签: javascript arrays reduce

var data = [{
    id: '1',
    status: 'pending'
}, {
    id: '2',
    status: 'delivered'
}, {
    id: '2',
    status: 'pending'
}, {
    id: '1',
    status: 'shipped'
}, {
    id: '1',
    status: 'pending'
}, {
    id: '2',
    status: 'delivered'
}, {
    id: '2',
    status: 'shipped'
}]



output: [{
    id: 1,
    order: {
        pending: 2,
        shipped: 1
    },
    id: 2,
    order: {
        pending: 1,
        delivered: 2,
        shipped: 1
    }
}]

2 个答案:

答案 0 :(得分:2)

使用Array reduce& amp; amp; amp;找到。

var ret = data.reduce(function(o, v) {
  //change id to Number
  var idNum = Number(v.id);

  var ele = o.find(function(e, i) {
    return idNum === e.id;
  });
  if (!ele) {
    //ID is not there in our output, let's create it
    ele = {
      id: idNum,
      order: {}
    };
    o.push(ele);
  }
  //update order status
  //if it's empty start with zero and increase it
  ele.order[v.status] = (ele.order[v.status] || 0) + 1;
  return o;
}, []);
console.log(JSON.stringify(ret));

答案 1 :(得分:1)

使用Array#reduce创建ID为密钥的对象和要转换的Object#values()



var data = [{
  id: '1',
  status: 'pending'
}, {
  id: '2',
  status: 'delivered'
}, {
  id: '2',
  status: 'pending'
}, {
  id: '1',
  status: 'shipped'
}, {
  id: '1',
  status: 'pending'
}, {
  id: '2',
  status: 'delivered'
}, {
  id: '2',
  status: 'shipped'
}]

let res = Object.values(
  data.reduce((a, c) => {
    if (!a[c.id]) {
      a[c.id] = {id: c.id, order: {pending:0, shipped:0, delivered:0}};
    }
    a[c.id].order[c.status] ++;
    return a
  }, {})
);

console.log(res)




回到阵列: