JavaScript How to correctly delete an object from an array by name?

How do I delete the created object "bird" from an array by name ie by the keyword "bird"?

var storage = [
    {cat: {name: "Garfild", count: 3443, price: 1000}}
];

function addProduct(newProduct) {
    storage.push(newProduct);
}

addProduct({dog: {name: "Rex", count: 1488, price: 2000}});
addProduct({bird: {name: "Eagle", count: 4042, price: 3000}});

function deleteProductByName(productName) {
    storage.remove(productName);
}
deleteProductByName("bird");
Answer:1

You could iterate from the end of the array and splice the found item, which uses a check if the property exists in the object.

function addProduct(newProduct) {
    storage.push(newProduct);
}

function deleteProductByName(productName) {
    var i = storage.length;
    while (i--) {
        if (productName in storage[i]) {
            storage.splice(i, 1);
        }
    }
}

var storage = [{ cat: { name: "Garfild", count: 3443, price: 1000 } }];

addProduct({ dog: { name: "Rex", count: 1488, price: 2000 } });
addProduct({ bird: { name: "Eagle", count: 4042, price: 3000 } });

deleteProductByName("bird");
console.log(storage);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:2

You may do it like:

storage = storage.filter(item => Object.keys(item)[0] !== 'bird');
Answer:3

You can write the remove like this if filtering is not what you want but really remove the data to the original array:

var storage = [
    {cat: {name: "Garfild", count: 3443, price: 1000}}
];

function addProduct(newProduct) {
    storage.push(newProduct);
}

addProduct({dog: {name: "Rex", count: 1488, price: 2000}});
addProduct({bird: {name: "Eagle", count: 4042, price: 3000}});

function deleteProductByName(productName) {
    var index=storage.map(function(e) { return Object.keys(e).join(); }).indexOf(productName);
   storage.splice(index,1)
}
deleteProductByName("bird");

console.log(storage)
Answer:4

If I create a inner class in a constructor, does it allocate memeory for each instance of the outer class that I create? For example, class PriorityQueue { constructor(maxSize) { // Set default ...

If I create a inner class in a constructor, does it allocate memeory for each instance of the outer class that I create? For example, class PriorityQueue { constructor(maxSize) { // Set default ...

I've come across this snippet and I have no idea why it does what it does. Try to answer the question yourself before looking at my answer below the code: what number will this alert? var foo = 1; ...

I've come across this snippet and I have no idea why it does what it does. Try to answer the question yourself before looking at my answer below the code: what number will this alert? var foo = 1; ...

How to get value from DB to the on-change event? Here is the example $("#widgetpanel").html(' >data="http://localhost:8080/guest?key='+$(this).val()+'item=??">'); I'm getting key from the drop-...

How to get value from DB to the on-change event? Here is the example $("#widgetpanel").html(' >data="http://localhost:8080/guest?key='+$(this).val()+'item=??">'); I'm getting key from the drop-...

  1. value change from
  2. value change dump
  3. value change management
  4. value chain analysis
  5. value change angular
  6. value change jquery
  7. value change listener java
  8. value change event

I have an array that I need to iterate through to get a value. someFunc: function(item) { var array = Ext.select(".item").elements; var elements; var itemId = item....

I have an array that I need to iterate through to get a value. someFunc: function(item) { var array = Ext.select(".item").elements; var elements; var itemId = item....