Skip to content
Advertisement

How to make object which store unique key have multiple different values

    var data = [{type: 'physical', value: 'verified'}, 
                {type: 'reference', value: 'owner'},
                {type: 'physical', value: 'pending'},
                {type: 'document', value: 'pending'}
               ]

How to return object in such a way which should have unique key which store mulpltiple values

Expected Result =

  var data  = {
      physical: ['verified', 'pending'],
      reference: ['owner'],
      document: ['pending']
  }

Advertisement

Answer

this function should return the result as you asked.

function getUnique(data){
    let x = {};
    for(let i of data){
        if (!x[i.type]){x[i.type] = [];}
        x[i.type].push(i.value);
    }
    return x;
}
Advertisement