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

177
Views
convert array into object name and values

I am trying to place my _rows values into my object value , however i have tried running a loop but i wasnt able to get the data out , the script i used is

    let _columns = ["Name", "age", "phone"]
    let _rows = [[{ value: 'john' }, { value: '22' }, { value: '999' }], [{ value: 'Bob' }, { value: '21' }, { value: '222' }]]

    let res = {}
    for (let i = 0; i < _rows.length; i++) {
        for (let a = 0; a < _columns.length; a++) {
            console.log(_rows[i][a].value)
        }
        res = _columns.reduce((acc,curr)=> (acc[curr]="data",acc),{});
        console.log(res)
        res = {}
    }

on my console.log , it prints

enter image description here

The target output should be

{Name: 'john', age: '22', phone: '999'}
{Name: 'bob', age: '21', phone: '222'}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can use

  • array.map to build another array from _rows

  • foreach value you have to check if columns exist

    if (_columns[index]) {
    
  • if yes add in the new object the value under column name

    res[_columns[index]] = property.value;
    

let _columns = ["Name", "age", "phone"]
let _rows = [[{ value: 'john' }, { value: '22' }, { value: '999' }], [{ value: 'Bob' }, { value: '21' }, { value: '222' }]];

let result = _rows.map(one => {
  let res = {};
  one.forEach((property, index) => {
    if (_columns[index]) {
      res[_columns[index]] = property.value;
    }
  });
  return res;
});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

If you want to stick with reduce, just make this small change to get the actual value instead of the hardcode string "data". You need to use the optional parameter currentIndex of the reduce callback, then use the index to access the row item for the value.

let _columns = ["Name", "age", "phone"]
let _rows = [[{ value: 'john' }, { value: '22' }, { value: '999' }], [{ value: 'Bob' }, { value: '21' }, { value: '222' }]]

  let res = {}
  for (let i = 0; i < _rows.length; i++) {
      for (let a = 0; a < _columns.length; a++) {
          console.log(_rows[i][a].value)
      }
      res = _columns.reduce((acc,curr,index)=> (acc[curr]=_rows[i][index].value,acc),{});
      console.log(res)
      res = {}
  }

However, when dealing with rows and columns in your case, since the array index is important, using old-school nested for-loop might be much more clear sometimes. My experience is, when you need the array index anyway, try not to be fancy and go for regular for-loop.

let _columns = ["Name", "age", "phone"]
let _rows = [[{ value: 'john' }, { value: '22' }, { value: '999' }], [{ value: 'Bob' }, { value: '21' }, { value: '222' }]]


for(let i = 0; i < _rows.length; i++)
{
  let res = {};
  for(let j = 0; j < _columns.length; j++)
  {
    res[_columns[j]] = _rows[i][j].value
  }
  console.log(res);
}

about 4 years ago · Juan Pablo Isaza Report

0

A double Arry.map will do the trick

let _columns = ["Name", "age", "phone"]
let _rows = [
  [{ value: 'john' }, { value: '22' }, { value: '999' }],
  [{ value: 'Bob' }, { value: '21' }, { value: '222' }]
];
const output = _rows.map((row) => row.map((item, index) => ({ [_columns[index]]: item.value })));
console.log(output)

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!