How to delete element from indexed array based on value
Example:
var add = { number1: 'hello' , number2: "Haii", number3: "Byee" };
Now I want to delete element which having value Haii.
Can we do it with out iterate using for loop.
Can we do it with out iterate using fir loop.
First, get all the keys which correspond to the value Haii.
var filteredKeys = Object.keys(add).filter(function(currentKey) {
return add[currentKey] === "Haii";
});
Then, delete all those keys from add
filteredKeys.forEach(function(currentKey) {
delete add[currentKey];
});
No explicit looping at all :-)
We can reduce the above seen two step process into a one step process, like this
Object.keys(add).forEach(function(currentKey) {
if (add[currentKey] === "Haii") {
delete add[currentKey];
}
});
Again, no explicit looping :-)