Delete last object from the array of objects.
21,236
Solution 1
You could just splice out the last element in the array:
obj.Results.splice(-1);
var obj = {
Results: [{
id: 1,
name: "Rick",
Value: "34343"
}, {
id:2,
name: 'david',
Value: "2332",
}, {
id: 3,
name: 'Rio',
Value: "2333"
}]
};
obj.Results.splice(-1);
console.log(obj);
Solution 2
Try using the .pop()
method. It'll delete the last item of an array.
obj.Results.pop();
Solution 3
You can use Array.prototype.splice
to remove the last item.
var data = {
Results : [ {
id : 1,
name : "Rick",
Value : "34343"
}, {
id : 2,
name :'david',
Value : "2332"
}, {
id : 3,
name : 'Rio',
Value : "2333"
}]
};
var removed = data.Results.splice(-1,1);
document.body.innerHTML = '<pre>'+ JSON.stringify(data, null, 4) +'</pre>'
Solution 4
You should use array.pop() for it, it removes the last element and returns it.
Solution 5
You can pop()
the last item out of the array.
obj.Results.pop()
For more on array methods, visit this.
Author by
Akanksha Iyer
Updated on September 22, 2020Comments
-
Akanksha Iyer over 3 years
I have this array of objects and i would like to delete the last object. i.e. 2 from the list. Can someone please let me know to do this.
Object {Results:Array[3]} Results:Array[3] [0-2] 0:Object id=1 name: "Rick" Value: "34343" 1:Object id=2 name:'david' Value: "2332" 2:Object id=3 name: 'Rio' Value: "2333"