Home > Software design >  merge array JSON return from API in app script
merge array JSON return from API in app script

Time:04-26

I using app script to get JSON array from API the array:

var a = '[{numbers=228, id=4152, gendar=female}, {numbers=978, id=8479, gendar=male}, {numbers=101, id=8479, gendar=male}, {numbers=356, id=4152, gendar=female}]';

I want to merge every array has same id. the return need like that:

[{numbers=584, id=4152, gendar=female}, {numbers=779, id=8479, gendar=male}]

CodePudding user response:

You can use Array.prototype.reduce() to group by id and then
use Object.values on the result like so:

const arr = [
  {numbers: 228, id: 4152, gendar: 'female'},
  {numbers: 978, id: 8479, gendar: 'male'},
  {numbers: 101, id: 8479, gendar: 'male'}, 
  {numbers: 356, id: 4152, gendar: 'female'}
];


const res = arr.reduce((acc, curr) => {
  if(acc[curr.id]) {
    acc[curr.id].numbers  = curr.numbers;
  } else {
    acc[curr.id] = curr
  }

  return acc;
}, {});

console.log(Object.values(res));

CodePudding user response:

Merge Objects

function merge() {
  const arr = JSON.parse(Your JSON goes here);
  const x = arr.reduce((a, c, i) => {
    let p = `${c.id}/${c.gendar}`;
    if (!a.obj.hasOwnProperty(p)) {
      a.obj[p] = c;
      a.obj.pA.push(p);
    } else {
      a.obj[p].numbers  = Number(c.numbers);
    }
    return a;
  }, { obj: { pA: [] }, oA: [], getArray: function () { this.obj.pA.forEach(p => { this.oA.push(this.obj[p]) });return this.oA; } }).getArray();
  Logger.log(JSON.stringify(x));
}

Generate data

function lfunkdata() {
  let arr = [...Array.from(new Array(Math.floor((Math.random() * 20)   5)).keys(), x => {
    let a = Math.floor((Math.random() * 9000)   10);
    let b = Math.floor((Math.random() * 10)   1);
    let c = Math.round(Math.random() * 1) ? "Male" : "Female";
    return { numbers: a, id: b, gendar: c };
  })];
  let r = JSON.stringify(arr)
  Logger.log(r);
  return r;
}

Simulated Data:

[{"numbers":5115,"id":9,"gendar":"Female"},{"numbers":6598,"id":8,"gendar":"Female"},{"numbers":162,"id":10,"gendar":"Female"},{"numbers":691,"id":8,"gendar":"Male"},{"numbers":684,"id":8,"gendar":"Female"},{"numbers":6941,"id":6,"gendar":"Male"},{"numbers":5822,"id":10,"gendar":"Male"},{"numbers":7453,"id":9,"gendar":"Female"},{"numbers":2663,"id":7,"gendar":"Male"},{"numbers":4031,"id":7,"gendar":"Female"},{"numbers":8581,"id":7,"gendar":"Male"},{"numbers":8940,"id":3,"gendar":"Male"},{"numbers":3622,"id":4,"gendar":"Male"},{"numbers":1842,"id":8,"gendar":"Female"},{"numbers":1653,"id":9,"gendar":"Male"},{"numbers":2085,"id":3,"gendar":"Female"},{"numbers":918,"id":5,"gendar":"Female"},{"numbers":2470,"id":7,"gendar":"Female"},{"numbers":8169,"id":4,"gendar":"Male"},{"numbers":754,"id":6,"gendar":"Female"},{"numbers":7444,"id":1,"gendar":"Male"},{"numbers":6094,"id":1,"gendar":"Male"}]

results:

[{"numbers":12568,"id":9,"gendar":"Female"},{"numbers":9124,"id":8,"gendar":"Female"},{"numbers":162,"id":10,"gendar":"Female"},{"numbers":691,"id":8,"gendar":"Male"},{"numbers":6941,"id":6,"gendar":"Male"},{"numbers":5822,"id":10,"gendar":"Male"},{"numbers":11244,"id":7,"gendar":"Male"},{"numbers":6501,"id":7,"gendar":"Female"},{"numbers":8940,"id":3,"gendar":"Male"},{"numbers":11791,"id":4,"gendar":"Male"},{"numbers":1653,"id":9,"gendar":"Male"},{"numbers":2085,"id":3,"gendar":"Female"},{"numbers":918,"id":5,"gendar":"Female"},{"numbers":754,"id":6,"gendar":"Female"},{"numbers":13538,"id":1,"gendar":"Male"}]

CodePudding user response:

Probably something like this:

var jsn = [
    { numbers: '228', id: '4152', gendar: 'female' },
    { numbers: '356', id: '4152', gendar: 'female' },
    { numbers: '978', id: '8479', gendar: 'male' },
    { numbers: '101', id: '8479', gendar: 'male' },
];

var obj = {};
for (let o of jsn)
  try { obj[o.id].numbers =  obj[o.id].numbers    o.numbers }
  catch(e) { obj[o.id] = o }

var arr = Object.values(obj);
console.log(arr);
Output:

[
  { numbers: 584,  id: '4152', gendar: 'female' },
  { numbers: 1079, id: '8479', gendar: 'male' }
]
  • Related