I've an array of object and check for property values and based on it i need to perform some operation
const arr= [
{
"id": "1",
"name": "Skoda - Auto",
"operation": "ADD"
},
{
"id": "2",
"name": "BMW - Auto",
"operation": "DELETE"
},
{
"id": "3",
"name": "Mustang",
"operation": "DELETE"
}
]
if (arr.some(e => e.operation === 'ADD')) {
console.log('will do some operation if its add')
}
else if(arr.some(e => e.operation === 'DELETE')) {
console.log('will do some operation if its delete')
}
else {}
I tried the above way but it just does not enter else if block, not sure what wrong I'm doing, can someone plz help on this
I think what you're actually looking for is a simple loop that does a check forEach()
item car
in your array arr
. .some()
checks whether any item in your array matches a given condition.
const arr = [
{
id: "1",
name: "Skoda - Auto",
operation: "ADD"
},
{
id: "2",
name: "BMW - Auto",
operation: "DELETE"
},
{
id: "3",
name: "Mustang",
operation: "DELETE"
}
];
arr.forEach(car => {
if (car.operation === "ADD") {
console.log("will do some operation if its add");
} else if (car.operation === "DELETE") {
console.log("will do some operation if its delete");
} else {
// implementation
}
});