Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda