如何删除最后一行对象

时间:2017-05-26 12:25:45

标签: javascript jquery

如何从对象中删除最后一条记录

var a = this.data();
console.log(a);
删除前

1) Object { type="marker",  categories=[1],  coords=[2],  more...}  
2) Object { type="marker",  categories=[1],  coords=[2],  more...}
3) Object { type="marker",  coords=[2],  ident="place",  more...}

我想删除3号或2号或1号

删除后

1) Object { type="marker",  categories=[1],  coords=[2],  more...}  
2) Object { type="marker",  categories=[1],  coords=[2],  more...}

BB.gmap.marker.prototype.display = function() {
    var a = this.data();
    a.splice(-1);
    console.log(a)
    if ("object" != typeof a.coords)
        return this.error("Requires coordinates [lat, lng] at BB.gmap.marker.display()"),
        !1;
    var b = {
        map: this.controller().map(),
        position: new google.maps.LatLng(a.coords[0],a.coords[1]),//27.6648274 -81.51575350000002
        optimized: !1
    };
}

2 个答案:

答案 0 :(得分:2)

我假设您有以下对象数组:[{},{},{}]

所以需要这样做: -

a.splice(-1);

实施例: -



var a = [{type: "marker",categories: [1],coords: [2]}, {type: "marker", categories: [1],coords: [2],}, {type: "marker", categories: [1],coords: [2]}]; // i think you have array of object

a.splice(-1);
console.log(a);




答案 1 :(得分:0)

我相信你没有正确地提出这个问题。我从你的问题中理解的是你有一个对象数组,你想要从中删除最后一个对象。为此,您可以这样做:

var arrayOfObjects = [{type="marker",  categories=[1],  coords=[2],  more...},
                       {type="marker",  categories=[1],  coords=[2],  more...}];

arrayOfObjects.splice(-1);
相关问题