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

394
Views
Merge objects into array

I want to convert multiple objects with same id as test to array of objects

Actual:

const array= [
    { "test": 1},
    { "test": 2},
 { "test": 3},
 { "test": 4},
]

Expected:

test: [1,2,3,4]

Can someone please help

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Just use the native method map (read more here or here) like this:

const array= [
  { "test": 1},
  { "test": 2},
  { "test": 3},
  { "test": 4},
];
const newArray = array.map(p => p.test);
console.log(JSON.stringify(newArray)); //[1,2,3,4]

Hope this helps.. ;D

about 4 years ago · Juan Pablo Isaza Report

0

You can use the map function for this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map :

const newarray = array.map(x => x.test);
console.log(newarray);
about 4 years ago · Juan Pablo Isaza Report

0

const result = {};

yourArray.forEach( ( object ) => {
  const keys = Object.keys( object );
  for ( let i = 0; i < keys.length; i++ ) {
     const key = keys[ i ];
     if ( ! key in result ) { 
        result[ key ] = [];
     }
     result[key] = [...result[key], ...object[key] ];
  }
});

console.log( result );

In this case:

  1. You need to iterate in your array
  2. On each iteration you have a new object
  3. From there you iterate in all the keys of that current object
  4. If the key does not exists in your result create a new empty array
  5. Merge the value from the result with the current value of the object.
  6. Values are stored in result.
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!