Akanksha Iyer Akanksha Iyer - 2 months ago 6
Javascript Question

Delete last object from the array of objects.

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"

Answer

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);