2

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.

2
  • snacks is an object. how do you get the value from it? Commented Mar 24, 2019 at 18:55
  • Hi @NinaScholz, I've simplified the code to focus on my problem. Let's say that I want whatever I find in fruits to match the value of snack. Thanks for your help Commented Mar 24, 2019 at 18:59

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.