简体   繁体   中英

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 eg, 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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM