简体   繁体   中英

How can I get to an array nested inside an object

I have an array of objects and within those objects there is a nested object with an array that i am trying to evaluate. It looks something like this:

item = [
{firstName: "John"
 lastName: "Johnson"
 capabilities: {demographic: Array(3), geographic: Array(3), language: Array(2)}
id: "ndja1-234-njk"
state: LA
}
{firstName: "Jane"
 lastName: "Johnson"
 capabilities: {demographic: Array(3), geographic: Array(3), language: Array(2)}
id: "ndsa1-234-njk"
state: TX
}
]

I am trying to filter the list of objects and only return the objects which match the users input for either firstname, lastname, phonenumber, and geographic . But because the geographic is not at the first level of the array I am getting an error that says is is null when i try and filter through it like this:

const newList = (items, value) => {
    let providerList = items.filter(i => i.firstName.includes(value) || i.lastName.includes(value) || i.phone.includes(value) || i.capabilites.geographic.includes(value));
    setSearchResult(providerList)
  };

I believe it is because of the position of the geographic values in the object but im not sure how to handle waiting for it to not be null. This function works until I add the geographic OR statement.

Try this solution. That can be the issue coz some objects don't include capabilities in it.

const newList = (items, value) => {
    let providerList = items.filter(i => i.firstName.includes(value) || i.lastName.includes(value) || i.phone.includes(value) || i.capabilites.geographic && i.capabilites.geographic.includes(value));
    setSearchResult(providerList)
  };

You have a typo. Instead of i.capabilites use i.capabilities

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