[Solved] can its possible to make for loop for array of objects, removing duplicate and add the values which consists of same user [closed]


You could try something like this:

var data = [
  { user: 'VAY9090', value: [ 'KL65' ] },
  { user: 'VAY9090', value: [ 'KL6I' ] },
  { user: 'VAY9092', value: [ 'KLMF' ] },
  { user: 'VAY9092', value: [ 'KLMQ' ] },
  { user: 'VAY9090', value: [ 'KLMR' ] },
  { user: 'BTD9891', value: [ 'KLMS' ] },
  { user: 'VAY9090', value: [ 'KLVZ' ] },
  { user: 'VAY9033', value: [ 'KMYJ' ] },
  { user: 'BTD9891', value: [ 'KMYK' ] }
];

var convertedData = convert(data);

console.log(convertedData);

function convert(data) {
  var result = [];
  var obj = {};

  data.forEach(function(item) {
    obj[item.user] = obj[item.user] || {
      user: item.user,
      value: []
    };

    obj[item.user].value = obj[item.user].value.concat(item.value);
  });

  for(var i in obj) {
    result.push(obj[i]);
  }

  return result;
}

1

solved can its possible to make for loop for array of objects, removing duplicate and add the values which consists of same user [closed]