Attaching Dynamic Variable in ES6 [closed]

This is not correct usage of find in array. Look at the sample:

products = ["vegetable", "food", "test"];
const productValue = 1;

var res = products.find((item => console.log(item)));

Now look at this one:

products = ["vegetable", "food", "test"];
const productValue = 1;

var res = products.find((item => console.log(item[1])));

See you are using "vegetable"[1] that is first character of each string

You know the index so easily check in if products[productValue] == "food"; Or of you want find by index use findIndex.

Upadte

After chattin with OP I understand he wants to append the productValue to one of products key value (option that is string), so you need to use + operator:

products.find((item => (item.option + productValue) === 'food'))

Leave a Comment