按字段值对数组进行分组,然后将数组中的分组值推入

时间:2019-06-13 12:44:59

标签: javascript arrays object group-by

我有一个数组:

let docArray = [ 
    { documentID: 304717110, doublettenID: 111, done: true },
  { documentID: 304717110, doublettenID: 222, done: true },
  { documentID: 304717110, doublettenID: 333, done: true },
  { documentID: 304717110, doublettenID: 444, done: true },
  { documentID: 304717110, doublettenID: 555, done: true },
  { documentID: 304717110, doublettenID: 666, done: true },
  { documentID: 304862280, doublettenID: null, done: true },
  { documentID: 304862281, doublettenID: null, done: true },
  { documentID: 304933685, doublettenID: null, done: true },
  { documentID: 304933871, doublettenID: null, done: true },
  { documentID: 304933870, doublettenID: null, done: true },
  { documentID: 304862282, doublettenID: null, done: true } 
  ]

我想按documentID分组,并为每个不同的documentID创建一个对象,然后将“ doublettenID”字段值推入每个对象的数组内。我如何轻松地做到这一点?

3 个答案:

答案 0 :(得分:1)

您可以遍历它,并使用documentID作为键,然后推到单独的对象。

let docArray = [ 
  { documentID: 304717110, doublettenID: 111, done: true },
  { documentID: 304717110, doublettenID: 222, done: true },
  { documentID: 304717110, doublettenID: 333, done: true },
  { documentID: 304717110, doublettenID: 444, done: true },
  { documentID: 304717110, doublettenID: 555, done: true },
  { documentID: 304717110, doublettenID: 666, done: true },
  { documentID: 304862280, doublettenID: null, done: true },
  { documentID: 304862281, doublettenID: null, done: true },
  { documentID: 304933685, doublettenID: null, done: true },
  { documentID: 304933871, doublettenID: null, done: true },
  { documentID: 304933870, doublettenID: null, done: true },
  { documentID: 304862282, doublettenID: null, done: true } 
];
let groups = {};

docArray.forEach(entry => {
    if (!groups.hasOwnProperty(entry.documentID)) {
	groups[entry.documentID] = [];
    }

    groups[entry.documentID].push(entry.doublettenID);
});

console.log(groups);

答案 1 :(得分:1)

这是一种实现方法,我不确定这是否是您需要的对象格式。

let docArray = [ 
    { documentID: 304717110, doublettenID: 111, done: true },
  { documentID: 304717110, doublettenID: 222, done: true },
  { documentID: 304717110, doublettenID: 333, done: true },
  { documentID: 304717110, doublettenID: 444, done: true },
  { documentID: 304717110, doublettenID: 555, done: true },
  { documentID: 304717110, doublettenID: 666, done: true },
  { documentID: 304862280, doublettenID: null, done: true },
  { documentID: 304862281, doublettenID: null, done: true },
  { documentID: 304933685, doublettenID: null, done: true },
  { documentID: 304933871, doublettenID: null, done: true },
  { documentID: 304933870, doublettenID: null, done: true },
  { documentID: 304862282, doublettenID: null, done: true } 
  ]
  
  var ids = []
  var res = []
  
  docArray.forEach(doc => {   
    if (!ids.includes(doc.documentID)) {
    	doc.doublettenID = [doc.doublettenID]
  		res.push(doc)
      ids.push(doc.documentID)
    } else {
    	res.find(d => doc.documentID = d.documentID).doublettenID.push(doc.doublettenID)
    }
 })
  
console.log(res)

答案 2 :(得分:1)

我建议您使用Array#reduce函数。以我的理解,它看起来很可读。

let docArray = [{
    documentID: 304717110,
    doublettenID: 111,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 222,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 333,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 444,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 555,
    done: true
  },
  {
    documentID: 304717110,
    doublettenID: 666,
    done: true
  },
  {
    documentID: 304862280,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304862281,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304933685,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304933871,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304933870,
    doublettenID: null,
    done: true
  },
  {
    documentID: 304862282,
    doublettenID: null,
    done: true
  }
];

const ar = docArray.reduce((a, c) => {
  debugger;
  let filtered = a.filter(el => el.documentID === c.documentID);
  if (filtered.length > 0) {
    a[a.indexOf(filtered[0])].doublettenID.push(c.doublettenID);
  } else {
    if (c.doublettenID !== null) {
      let originalValue = c.doublettenID;
      c.doublettenID = [];
      c.doublettenID.push(originalValue);
    }

    a.push(c);
  }
  return a;
}, []);

$('#pre').text(JSON.stringify(ar));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<pre id='pre'></pre>