1

this is my input, i have an array where name key has value 'foo1' in multiple objects so i want unique object with respect to 'name' key value and other information on a separate array.

var array = [{
    name: "foo1",
    value: "val1",
    status: "1"
  }, {
    name: "foo1",
    value:  "val2",
    status: "0"
  }, {
    name: "foo1",
    value:  "val3",
    status: "1"
  }, {
    name: "foo2",
    value: "val4",
    status: "1"
  }];

and i want on output like below. Below array are unique by name key and other value and status key from all matching object store on new_obj key.

var output = [{
  name: "foo1",
  new_obj: [{
      value:"val1", 
      status: "1"
    }, {
      value:"val2", 
      status: "0"
    }, {
      value:"val3", 
      status: "1"
    }]
  }, {
    name: "foo2",
    new_obj: [{
      value:"val4", 
      status: "1"
    }]  
  }];
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
Binod
  • 23
  • 2
  • 3
    Welcome to Stack Overflow! Please note that Stack Overflow is not a code writing service. You have to attempt this yourself, and if you encounter any problems, we will be glqad to help. – Wais Kamal Aug 12 '20 at 13:28
  • Take a look at this to understand how to ask a good question: https://stackoverflow.com/help/how-to-ask – Kitson Aug 12 '20 at 13:48
  • This looks like a duplicate: https://stackoverflow.com/questions/36344976/collect-unique-objects-in-javascript-array – Kitson Aug 12 '20 at 13:50
  • Does this answer your question? [Collect unique objects in JavaScript array](https://stackoverflow.com/questions/36344976/collect-unique-objects-in-javascript-array) – Kitson Aug 12 '20 at 13:50

3 Answers3

1
var output = array.reduce((acc, value) => {
  if (!acc[value.name]) acc[value.name] = {
    name: value.name,
    new_obj: []
  };
  acc[value.name].new_obj.push({value: value.value, status: value.status});
  return acc;
}, {});
siyb
  • 2,837
  • 2
  • 21
  • 18
1

You may build up (e.g. using Array.prototype.reduce()) the Map, having name values as keys and grouped objects as values, so once you find the name that was already seen (Map.prototype.get() does not return undefined by respective key), you push the rest of properties as a separate object to new_obj, if encountered name was not seen before, you create (with Map.prototype.set()) grouped object for that.

As your Map is ready, you extract its values with Map.prototype.values() into array, using spread syntax:

const arr = [{name:"foo1",value:"val1",status:"1"},{name:"foo1",value:"val2",status:"0"},{name:"foo1",value:"val3",status:"1"},{name:"foo2",value:"val4",status:"1"}],
    
    result = [...arr
      .reduce((acc, {name, ...rest}) => {
          const group = acc.get(name)
          group ? group.new_obj.push(rest) : acc.set(name, {name, "new_obj":[rest]})
          return acc
        }, new Map)
      .values()
    ]
    
console.log(result)
.as-console-wrapper{min-height:100%;}
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
0

You can have a oneliner for this using lodash

_.groupBy(array, 'name')
Omkar Kulkarni
  • 1,091
  • 10
  • 22