Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
convert one array data to another data with new changes

I want to convert one data array to another array with new change means, if id match then push data into current one otherwise create new object with second id

let finaldata=[]

let data =[{id:1,name:"meet"},{id:2,name:"raj"},{id:1,name:"hari"},{id:3,name:"hari"},{id:2,name:"ram"}]

//I want this below array how can I archive this.?

finaldata= [ { id: 1, names: [ 'meet', 'hari' ] },
  { id: 2, names: [ 'raj', 'ram' ] },
  { id: 3, names: [ 'hari' ] }]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let finaldata = [];
let data =[{id:1,name:"meet"},{id:2,name:"raj"},{id:1,name:"hari"},{id:3,name:"hari"},{id:2,name:"ram"}];

data.forEach((e) => {
    let val = {};
    val.id = e.id;
  val.names = [e.name];

if (finaldata.length > 0) {
  let match = 0;
  
  for(i = 0; i < finaldata.length; i++) {
    if(finaldata[i].id == e.id){
      finaldata[i].names.push(e.name);
      match = 1;
    }
  }
  
  if(match == 0){
  finaldata.push(val);
  }
} else {
    finaldata.push(val);
}

});

console.log(finaldata);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!