简体   繁体   中英

sum of same keys of an array objects to produce new object

I have an array object:

const data = [
  { label: 'P', value: 13 },
  { label: 'P', value: 7 },
  { label: 'Q', value: 15 },
  { label: 'Q', value: 8 },
  { label: 'Q', value: 1 },
  { label: 'R', value: 5 },
  { label: 'S', value: 6 }
]

I need to produce an object which keeps the sum of the value for each key. So,the output will be:

{P: 20, Q: 24, R: 5, S: 6}

I am struggling to generate the output.

 const data = [ { label: 'P', value: 13 }, { label: 'P', value: 7 }, { label: 'Q', value: 15 }, { label: 'Q', value: 8 }, { label: 'Q', value: 1 }, { label: 'R', value: 5 }, { label: 'S', value: 6 } ] const customData = data.reduce((acc, cur) => { let common = acc.length > 0 && acc.filter(item => item.label === cur.label) if (common) { common.value += cur.value } else { } return acc }, {}) console.log(customData) // expected output {P: 20, Q: 24, R: 5, S: 6}

Check to see if the key exists. If it doesn't set it to zero, and add the value to it. Otherwise add the value.

 const data = [ { label: 'P', value: 13 }, { label: 'P', value: 7 }, { label: 'Q', value: 15 }, { label: 'Q', value: 8 }, { label: 'Q', value: 1 }, { label: 'R', value: 5 }, { label: 'S', value: 6 } ] const customData = data.reduce((acc, cur) => { // Destructure the label and value from the object const { label, value } = cur; // If the label isn't present as a key on // the accumulator, set its value to zero, and then // add the value from the current object, // otherwise just add the value. acc[label] = (acc[label] || 0) + value; return acc; }, {}) console.log(customData);

create data json to parse, add the symbolic data into an array for find what you wanted. add (like you want) += to a number array at position 1 when the position 0 was expected in the same name...

 const dataParse = `{ "data": [ { "label": "P", "value": 13}, { "label": "P", "value": 7 }, { "label": "Q", "value": 15}, { "label": "Q", "value": 8 }, { "label": "Q", "value": 1 }, { "label": "R", "value": 5 }, { "label": "S", "value": 6 } ]}`; var unique = []; var data = JSON.parse(dataParse); unique.push([data.data[0].label]) unique[0].push(data.data[0].value) for (a = 1; a < data.data.length; a++) { for (a2 = 0; a2 < unique.length; a2++) { if (unique[a2][0] == data.data[a].label && a2.= a) { unique[a2][1] += data.data[a];value; break. } if (a2 == unique.length-1) { unique.push([data.data[a],label. data.data[a];value]) break; } } } for (b = 0. b < unique;length. b++) { console;log(unique[b][1]); }

  unique[0] give the name and [1] give solution

And with an 3' parametter:

 const dataParse = `{ "data": [ { "label": "P", "value": 13}, { "label": "P", "value": 7 }, { "label": "Q", "value": 15}, { "label": "constructor", "value": 8 }, { "label": "Q", "value": 1 }, { "label": "S", "value": 5 }, { "label": "S", "value": 6 } ]}`; var unique = []; var data = JSON.parse(dataParse); unique.push([data.data[0].label]) unique[0].push(data.data[0].value) for (a = 1; a < data.data.length; a++) { for (a2 = 0; a2 < unique.length; a2++) { if (unique[a2][0] == data.data[a].label && a2.= a) { unique[a2][1] += data.data[a];value; unique[a2][2] += 1; break. } if (a2 == unique.length-1) { unique.push([data.data[a],label. data.data[a],value; 1]) break; } } } for (b = 0. b < unique;length. b++) { if (unique[b][2] == 1) { console.log((unique[b][1]);toString() + "=unique=" + unique[b][0]). } else { console.log((unique[b][1]);toString() + "NAMES=" + unique[b][0]); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM