简体   繁体   中英

Find Value in Nested Array

I'm trying to find a matching value within an array of objects.

I'm using find for the job, and that seems to work fine. But what if I need to assign more than one value to one of those keys?

Here's the current code:

const snack = "strawberry";

const fruits = [
    { label: "yellowFruit", value: "banana" },
    { label: "purpleFruit", value: "grape" },
    { label: "redFruit", value: "apple" },
    { label: "greenFruit", value: "waltermelon" },
];

And here's how I'm finding my value:

fruits.find(fruit => fruit.value === snack) || fruits[0]

I would actually need to associate two values to the label redFruit without duplicating that label, as shown below, but then find cannot do the job anymore.

Something like this:

const snack = "strawberry";

const fruits = [
    { label: "yellowFruit", value: "banana" },
    { label: "purpleFruit", value: "grape" },
    {
        label: "redFruit",
        value: [
            { val: "apple" },
            { val: "strawberry" }
        ]
    },
    { label: "greenFruit", value: "waltermelon" },
];

But finding strawberry with the below code doesn't match:

fruits.find(fruit => fruit.value === snacks) || fruits[0]

Any help would be greatly appreciated.

You have to use a different method based on the value property type, if the value is an array then use Array#some method to achieve the result.

let res = fruits.find(({ value }) => Array.isArray(value) ? value.some(({ val }) => val === snacks) : value === snacks) || fruits[0]

 const snacks = "strawberry"; const fruits = [{ label: "yellowFruit", value: "banana" }, { label: "purpleFruit", value: "grape" }, { label: "redFruit", value: [{ val: "apple" }, { val: "strawberry" } ] }, { label: "greenFruit", value: "waltermelon" }, ]; let res = fruits.find(({ value }) => Array.isArray(value) ? value.some(({ val }) => val === snacks) : value === snacks) || fruits[0] console.log(res); 

Keep data simple:

const snack = "strawberry";

const fruits = [
    { label: "yellowFruit", value: "banana" },
    { label: "purpleFruit", value: "grape" },
    { label: "redFruit", value: "apple" },
    { label: "redFruit", value: "strawberry" },
    { label: "greenFruit", value: "waltermelon" },
];

... and your code will be simple too:

const result = fruits.find(({ value }) => value === snack)

You can do so:

 const snack = "strawberry"; const fruits = [{ label: "yellowFruit", value: "banana" }, { label: "purpleFruit", value: "grape" }, { label: "redFruit", value: [{ val: "apple" }, { val: "strawberry" } ] }, { label: "greenFruit", value: "waltermelon" }, ]; let itemExists = fruits.some(item => Array.isArray(item.value) ? item.value.some(subItem => subItem.val === snack) : item.value === snack); console.log(itemExists); 
By doing it this way you will receive a boolean whether or not the item exists. Hope that helps,

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