Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda