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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x