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

137
Views
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 answers
Answer question

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 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!