如何按名称(即关键字“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');