chefcurry7 chefcurry7 - 1 month ago 23
Javascript Question

Check object for property

Lets say I have an object

var person = [{
name: 'true',
city: 'false'
}]


and I have a predefined list of property names I would like to check against

let props = ['name', 'address']


How can I use lodash _.includes to check?

I've tried variations of the following to no avail, all return false

_.includes(person, [props]);
_.includes(person, 'name');
_.includes([person], 'name');

Answer

-- UPDATED -- Given your updated description, this is what the code will look like to get the results you desire.

const person = [{
  name: 'true',
  city: 'false'
}];
const props = ['name','address'];
let found = [];

Object.keys(person[0]).forEach(key => {
    if(props.includes(key)) found.push(key)
});

// Found now contains ['name']

Comments