Reputation: 9
I am trying to remove certain items from an array with js
Here is my array
[
{
"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"
}
]
on my screen I have 4 delete buttons called "deletebutton_0,deletebutton_1,deletebutton_2,deletebutton_4
so for example if i click deletebutton_0 the js will remove all details of the first Item from the array,
{"Pizza":"Margarita","Size":"Twelve Inch Stuffed Crust","Base":"Deep Base","Price":"6.50"}
I'm a nob at js, still learning.
Upvotes: 0
Views: 159
Reputation: 41665
Well, there are a few options for removing items from an array...
If your item is the first item you could use Array.prototype.shift()
.
myArray.shift(); // removes and returns the first item
If it's the last item you could use Array.prototype.pop()
.
myArray.pop(); // removes and returns the last item
You could use delete myArray[0];
to simply remove the 0
property but that wouldn't re-index the array.
Generally, you'll use Array.prototype.splice()
to remove/insert items from/into an array from/at a particular location, i.e.:
myArray.splice(0, 1); // removes 1 item starting at index 0
Here's an example using 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>
Upvotes: 2
Reputation: 11707
Try
array.splice(index,1)
where array
is your array and index
is the object's index you want to remove
Upvotes: 2