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

136
Vistas
Get values from arrays inside nested objects, compare and return the object

I am trying to loop through a nested object that looks like this:

let obj = {
  cols: [
    { name: 'name', type: 'String' },
    { name: 'dob', type: 'Number' },
    { name: 'address', type: 'String' },
    { name: 'income', type: 'String' },
    { name: 'vehicleNumber', type: 'Number' },
    { name: 'assets', type: 'Number' }
  ],
  row: [ 
    { 
      name: 'randomInfo', 
      columns: ['name', 'address', 'assets'], 
    } 
  ]
}

I am using the logic below to loop through the object's arrays, compare if they are equal, and if they are, I am returning them in an array. I am trying to return the entire object inside the cols key though. For e.g, if there are matching elements inside cols array' name value with the row array's columns key's value, (cols.name === row.columns[element], if there is a match return cols object)

//loop through the obj and get the keys before this
let cols = cols.map(col => col.name);
let row = row.map(ind => ind.columns);
let rowNamesFlattened = [].concat.apply([], row);
let matchingCols = cols.filter(element => row.includes(element));

The matchingCols object now has the matching names, but I want to ultimately return their type as well. Any idea how this can be done?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you can use filter directly on the cols array. However here I assumed that row array has only 1 element

let obj = {
  cols: [
    { name: 'name', type: 'String' },
    { name: 'dob', type: 'Number' },
    { name: 'address', type: 'String' },
    { name: 'income', type: 'String' },
    { name: 'vehicleNumber', type: 'Number' },
    { name: 'assets', type: 'Number' }
  ],
  row: [ 
    { 
      name: 'randomInfo', 
      columns: ['name', 'address', 'assets'], 
    } 
  ]
}

let matchingCols = obj.cols.filter(({name}) => obj.row[0].columns.includes(name))

console.log(matchingCols)

In case multiple elements present inside row array. can use flatMap to get flattened list of columns and then the same procedure as above

let obj = {
  cols: [
    { name: 'name', type: 'String' },
    { name: 'dob', type: 'Number' },
    { name: 'address', type: 'String' },
    { name: 'income', type: 'String' },
    { name: 'vehicleNumber', type: 'Number' },
    { name: 'assets', type: 'Number' }
  ],
  row: [ 
    { 
      name: 'randomInfo', 
      columns: ['name', 'address', 'assets'], 
    },
    { 
      name: 'randomInfo2', 
      columns: ['dob','name'], 
    } 
  ]
}

let filtered = obj.cols.filter(({name}) => obj.row.flatMap(ind => ind.columns).includes(name))

console.log(filtered)

Another solution to get both matched and unmatched in one go using reduce. so no need 2 filter calls. referenced this

let obj = {
  cols: [
    { name: 'name', type: 'String' },
    { name: 'dob', type: 'Number' },
    { name: 'address', type: 'String' },
    { name: 'income', type: 'String' },
    { name: 'vehicleNumber', type: 'Number' },
    { name: 'assets', type: 'Number' }
  ],
  row: [ 
    { 
      name: 'randomInfo', 
      columns: ['name', 'address', 'assets'], 
    },
    { 
      name: 'randomInfo2', 
      columns: ['dob','name'], 
    } 
  ]
}

let flatted = obj.row.flatMap(ind => ind.columns);

const result = obj.cols.reduce((acc, curr) => {
    acc[flatted.includes(curr.name) ? 'match' : 'unmatch'].push(curr);
    return acc;
}, { match: [], unmatch: [] });

console.log(result)

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