This is my array:
const ids = [
"id1",
"id2",
"id3"
]
Object that is extracted via cookies
const data = [
{
id: "id3" // dynamically populated and can vary, we need this property
prop2: "prop2", // not needed
prop3: "prop3" // not needed
}
]
How can I compare first array values - ids
with data.id
, and if true
, show some logic (for example - console.log(true)
? I need it in if statement
.
Please note that data.id
. can be anything, from id1
to id3
. I need to find a way to dynamically compare these values. In "real world" it id anything from 1
do 50000
, but this is just a mock-up for the example.
Also I would appreciate Lodash example.
For each item in data, you check if the id is in the array of ids, and if yes, do something
const ids = [
"id1",
"id2",
"id3"
]
const data = [
{
id: "id3", // dynamically populated and can vary, we need this property
prop2: "prop2", // not needed
prop3: "prop3" // not needed
}
]
// if we have the id in ids array, do something
if (data.some(d => ids.indexOf(d.id) > -1)) {
// do something
}
Using lodash:
const ids = [
"id1",
"id2",
"id3"
]
const data = [
{
id: "id3", // dynamically populated and can vary, we need this property
prop2: "prop2", // not needed
prop3: "prop3" // not needed
}
]
// if we have the id in ids array, do something
if (_.some(data, d => ids.indexOf(d.id) > -1)) {
// do something
}