从数组中删除
delete from array of arrays
我正在尝试使用 js 从数组中删除某些项目
这是我的阵列
[
{
"Pizza": "Margarita",
"Size": "Twelve Inch Stuffed Crust",
"Base": "Deep Base",
"Price": "6.50"
},
{
"Pizza": "Hot Vegetarian",
"Size": "Twelve Inch",
"Base": "Deep Base",
"Price": "6.00"
},
{
"Pizza": "Vegetarian",
"Size": "Ten Inch Stuffed Crust",
"Base": "Deep Base",
"Pricelabel": "Price",
"Price": "6.50"
},
{
"Pizza": "Hot Vegetarian",
"Size": "Fourteen Inch Stuffed Crust",
"Base": "Deep Base",
"Pricelabel": "Price",
"Price": "10.50"
}
]
在我的屏幕上,我有4个名为"deletebutton_0,deletebutton_1,deletebutton_2,deletebutton_4的删除按钮
因此,例如,如果我单击deletebutton_0 JS将从数组中删除第一个项目的所有详细信息,
{"Pizza":"Margarita","Size":"Twelve Inch Stuffed Crust","Base":"Deep Base","Price":"6.50"}
我是js的贵族,仍在学习。
试试
array.splice(index,1)
其中array
是数组,index
是要删除的对象索引
好吧,有几个选项可以从数组中删除项目......
如果您的项目是第一个项目,您可以使用Array.prototype.shift()
.
myArray.shift(); // removes and returns the first item
如果这是最后一项,您可以使用Array.prototype.pop()
.
myArray.pop(); // removes and returns the last item
您可以使用 delete myArray[0];
简单地删除 0
属性,但这不会重新索引数组。
通常,您将使用Array.prototype.splice()
从/在特定位置从数组中删除/插入到数组中,即:
myArray.splice(0, 1); // removes 1 item starting at index 0
下面是一个使用 splice
的示例:
var data = [{
"Pizza": "Margarita",
"Size": "Twelve Inch Stuffed Crust",
"Base": "Deep Base",
"Price": "6.50"
}, {
"Pizza": "Hot Vegetarian",
"Size": "Twelve Inch",
"Base": "Deep Base",
"Price": "6.00"
}, {
"Pizza": "Vegetarian",
"Size": "Ten Inch Stuffed Crust",
"Base": "Deep Base",
"Pricelabel": "Price",
"Price": "6.50"
}, {
"Pizza": "Hot Vegetarian",
"Size": "Fourteen Inch Stuffed Crust",
"Base": "Deep Base",
"Pricelabel": "Price",
"Price": "10.50"
}];
// create a click handler for your delete buttons
function handler(e) {
// get array index
var index = parseInt(this.id.split("_")[1], 10);
// get the next sibling for reindexing
var sibling = this.nextElementSibling;
// remove the button from the DOM
this.parentNode.removeChild(this);
// remove item from Array
var removedItems = data.splice(index, 1);
// reindex remaining buttons
do {
sibling.textContent = sibling.id = "deletebutton_" + index;
} while (index++, sibling = sibling.nextElementSibling);
// log info
console.log("remaining items: %o, removed: %o", data.length, removedItems[0]);
}
// get a reference to your buttons
var buttons = document.querySelectorAll("button");
// Add the event handler to your buttons
Array.prototype.forEach.call(buttons, b => b.addEventListener("click", handler));
<button type="button" id="deletebutton_0">deletebutton_0</button>
<button type="button" id="deletebutton_1">deletebutton_1</button>
<button type="button" id="deletebutton_2">deletebutton_2</button>
<button type="button" id="deletebutton_3">deletebutton_3</button>
你想使用拼接,像这样:
array.splice(index, 1);
根据单击的按钮设置index
。
相关文章:
- Mongoose-在更新中删除数组元素
- 在特定索引处剥离/删除数组中的值
- Framework7:使用swipeout删除数组中的项
- Javascript删除数组中某个位置之后的项
- 通过选中和取消选中复选框来添加和删除数组中的值
- 编写删除数组并添加到列表的函数
- Javascript - 删除数组中的重复值
- 在 Kineticjs 中删除数组中的组
- 如何根据数组内容添加/删除数组中的元素
- 用于删除数组中的零的Javascript函数代码
- Javascript-仅使用pop()方法删除数组中的负值
- 删除数组元素并将它们添加回原来的位置
- 如果一个键匹配,如何删除数组元素
- 无法使用拼接删除数组中的元素
- 如何从ko.computed中删除数组项
- 删除数组中的重复项,为什么此代码不起作用
- 使用另一个数组按索引删除数组中的对象
- 在 jquery 中,如何通过索引[“键”] 或值删除数组元素
- 使用 Lodash 删除数组中的元素
- 按索引删除数组元素