0

I parsed data to a variable

var Mydata = [
  {
    count: 4,
    min: 0,
    max: 0,
    mean: 0,
    ID: 1
  },
  {
    count: 12,
    min: 0,
    max: 0,
    mean: 0,
    ID: 2
  },
  {
    count: 9,
    min: 0,
    max: 0,
    mean: 0,
    ID: 3
  },
  {
    count: 4,
    min: 0,
    max: 0,
    mean: 0,
    ID: 1
  }
];

I want to add array element if "ID" is same

expected:

Mydatanew = [
  {
    count: 8,
    min: 0,
    max: 0,
    mean: 0,
    ID: 1
  },
  {
    count: 12,
    min: 0,
    max: 0,
    mean: 0,
    ID: 2
  },
  {
    count: 9,
    min: 0,
    max: 0,
    mean: 0,
    ID: 3
  }
];
Anuradha Gunasekara
  • 6,553
  • 2
  • 27
  • 37

6 Answers6

0

You can do this.

lodashJS > uniqBy !!

_.uniqBy([
    {count: 4, min: 0, max: 0, mean: 0, ID: 1},
    {count: 12, min: 0, max: 0, mean: 0, ID: 2},
    {count: 9, min: 0, max: 0, mean: 0, ID: 3},
    {count: 4, min: 0, max: 0, mean: 0, ID: 1}
], 'ID');

reference: https://lodash.com/docs/4.17.10#uniqBy


but uniqBy can override previous value... just block add new item...

If you want to make the last element a default, sort desc..

seunggabi
  • 1,699
  • 12
  • 12
0

var Mydata = [
{count: 4, min: 0, max: 0, mean: 0, ID: 1},
{count: 4, min: 0, max: 0, mean: 0, ID: 1},
{count: 12, min: 0, max: 0, mean: 0, ID: 2},
{count: 9, min: 0, max: 0, mean: 0, ID: 3},
{count: 4, min: 0, max: 0, mean: 0, ID: 1}
];

var Mydata = Mydata.reduce(function(obj,val){
 obj[val.ID]?obj[val.ID].count += val.count:obj[val.ID] = val;
return obj;
},{});

Mydata = Object.values(Mydata);

console.log(Mydata);
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
0

var Mydata = [{count: 4, min: 0, max: 0, mean: 0, ID: 1},
{count: 12, min: 0, max: 0, mean: 0, ID: 2},
{count: 9, min: 0, max: 0, mean: 0, ID: 3},
{count: 4, min: 0, max: 0, mean: 0, ID: 1}];

var reducer = Mydata.reduce((acc,item)=>{
 if(acc[item.ID]){
  //add the items
  acc[item.ID].count += item.count;
 }
 else{
  acc[item.ID] = item;
 }
 return acc;

},{});

var newMyData = Object.values(reducer);
console.log(newMyData);
Avinash
  • 2,003
  • 2
  • 17
  • 15
0

@Nishant has almost got it right, but I found you need an if statement instead of the ternary operator:

Edited (thanks to @aytee for the tip on preventing mutation of original data!):

var Mydata = [{count: 4, min: 0, max: 0, mean: 0, ID:1},
{count: 12, min: 0, max: 0, mean: 0, ID: 2},
{count: 9, min: 0, max: 0, mean: 0, ID: 3},
{count: 4, min: 0, max: 0, mean: 0, ID: 1}];

var reformedData=Object.values(Mydata.reduce(function(obj,val){
 if (obj[val.ID]) obj[val.ID].count +=val.count; 
 else obj[val.ID] = Object.assign({},val); // create an independent "copy with same values"
return obj;
},{}));
console.log(reformedData); // result: the consolidated data object
console.log(Mydata);       // still there and unchanged: the original data object
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • Might want to replace `obj[val.ID] = val` with `obj[val.ID] = Object.assign({}, val)` if you don't want to mutate the original data. – aytee17 Sep 21 '18 at 06:11
0

var Mydata = [{
    count: 4,
    min: 0,
    max: 0,
    mean: 0,
    ID: 1
  },
  {
    count: 12,
    min: 0,
    max: 0,
    mean: 0,
    ID: 2
  },
  {
    count: 9,
    min: 0,
    max: 0,
    mean: 0,
    ID: 3
  },
  {
    count: 4,
    min: 0,
    max: 0,
    mean: 0,
    ID: 1
  }
];


var result = {}
var t_key = {
  'count': 1,
  'min': 1,
  'max': 1,
  'mean': 1
}
Mydata.forEach(function(f) {
  if (f['ID'] in result) {
    for (var key in t_key) {
      if (key == 'count') {
        result[f['ID']][key] = (result[f['ID']][key] + f[key])
      } else if (key == 'min') {
        if (f[key] < result[f['ID']][key]) {
          result[f['ID']][key] = f[key]
        }
      } else if (key == 'max') {
        if (f[key] > result[f['ID']][key]) {
          result[f['ID']][key] = f[key]
        }
      } else if (key == 'mean') {
        result[f['ID']][key] = (result[f['ID']][key] + f[key])
      }
    }
  } else {
    result[f['ID']] = f
  }
})
console.log('final_result', Object.values(result))

var Mydata = [{
    count: 4,
    min: 0,
    max: 0,
    mean: 0,
    ID: 1
  },
  {
    count: 12,
    min: 0,
    max: 0,
    mean: 0,
    ID: 2
  },
  {
    count: 9,
    min: 0,
    max: 0,
    mean: 0,
    ID: 3
  },
  {
    count: 4,
    min: 0,
    max: 0,
    mean: 0,
    ID: 1
  }
];


var result = {}
var t_key = {
  'count': 1,
  
}
Mydata.forEach(function(f) {
  if (f['ID'] in result) {
    for (var key in t_key) {
      result[f['ID']][key] = (result[f['ID']][key] + f[key])
    }
  } else {
    result[f['ID']] = f
  }
})
console.log( Object.values(result))
0

const MyData = [
  { count: 4, min: 0, max: 0, mean: 0, ID: 1 },
  { count: 12, min: 0, max: 0, mean: 0, ID: 2 },
  { count: 9, min: 0, max: 0, mean: 0, ID: 3 }
];

const NewDataAdd = { count: 100, min: 1, max: 2, mean: 0, ID: 6 };
const NewDataRewrite = { count: 100, min: 1, max: 2, mean: 0, ID: 1 };

const updateData = (data, update) => {
  let isNew = true;

  data.map(item => {
    // if object is allready in array, overwrites it
    if (item.ID === update.ID) {
       isNew = false;
       return update
    }
  })
  
  // otherwise adds it to array
  if (isNew) return [ ...data, update];

  return data;
}

const test1 = updateData(MyData, NewDataAdd);
const test2 = updateData(MyData, NewDataRewrite);// takes array and an object

console.log(test1);
console.log(test2);