-1

How to create object using pair values from another object in javascript

Input:

{
firstObject:{
{
"version":"1000",
"issue":"issue1"
},
{
"version":"1001",
"issue":"issue2"
},
{
"version":"1000",
"issue":"issue3"
}

}
}

Above is my input and I want output as following:

{
newObject:{
"1000":["issue1", "issue3"],
"1001":["issue2"]
}
}

Pavan
  • 35
  • 6

2 Answers2

0

You can try this

let input = {
  firstObject:[
  {
  "version":"1000",
  "issue":"issue1"
  },
  {
  "version":"1001",
  "issue":"issue2"
  },
  {
  "version":"1000",
  "issue":"issue3"
  } 
]
}

function createNewObject( input ){ 
  let output= {};
  input.firstObject.map(( item ) => {
    if( !output[ item.version ]){
      output[ item.version ] =[]
    }
    output[ item.version ].push( item.issue )
  })

  return({
    newObject: output
  })
}

console.log( createNewObject( input ))
GansPotter
  • 761
  • 5
  • 8
0

Your Input is not a valid JSON. firstObject should be an array instead of object.

Demo

var obj = {
 "firstObject": [{
   "version": "1000",
   "issue": "issue1"
  },
  {
   "version": "1001",
   "issue": "issue2"
  },
  {
   "version": "1000",
   "issue": "issue3"
  }
 ]
};

var newObject = {};

obj.firstObject.map((item) => {
    if( !newObject[ item.version ]){
      newObject[item.version] = [];
    }
    newObject[item.version].push(item.issue);
});

console.log({ newObject });
Debug Diva
  • 26,058
  • 13
  • 70
  • 123