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"
Try using the .pop()
method. It'll delete the last item of an array.
obj.Results.pop();
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);
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>'
You should use array.pop() for it, it removes the last element and returns it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With