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

159
Views
Converting an array of arrays into an object with key and value with javascript

method input: (['name', 'marcus'], ['address', 'New York']

method output: {name: marcus, address: New York}

How do i do it?

cont array = [['c','2'],['d','4']];

function objectify()
{
    array.forEach(arrayElement => { for(let i = 0; i<2; i++){
            const obj = Object.assign({array[i][i]}, array[i++])
    }
    return obj;
})

}

console.log(objectify);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use Object.fromEntries()

const data = [['name', 'marcus'], ['address', 'New York']];
const result = Object.fromEntries(data);
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

1) If you are using forEach then you don't have to use for loop inside it

array.forEach(arrayElement => { for(let i = 0; i<2; i++){
            const obj = Object.assign({array[i][i]}, array[i++])
    }

2) You shouldn't hardcode the length upto which i will run, because it can be of any length.

for(let i = 0; i<2; i++)

If you are using forEach then you should loop over the array and get the key and value and set it into the resultant object.

function objectify(array) {
  const result = {};
  
  array.forEach((o) => {
    const [key, value] = o;
    result[key] = value;
  });

  return result;
}

const array = [
  ["c", "2"],
  ["d", "4"],
];
console.log(objectify(array));

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!