gpt4 book ai didi

javascript - 如何按名称从数组中正确删除对象?

转载 作者:行者123 更新时间:2023-12-02 08:07:41 24 4
gpt4 key购买 nike

如何按名称(即关键字“bird”)从数组中删除创建的对象“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");

最佳答案

你可以这样做:

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

关于javascript - 如何按名称从数组中正确删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50087217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com