如何一个js采用键值对的方法,该怎么删除一个元素呢
var json = new Array();
json['marker1'] = "111";
json['marker2'] = "222";
json['marker3'] = "333";
我想删除下表为marker1的元素该怎么删除,求解
------解决方案--------------------
shift:删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined
var a = [1,2,3,4,5];
var b = a.shift(); //a:[2,3,4,5] b:1
pop:删除原数组最后一项,并返回删除元素的值;如果数组为空则返回undefined
var a = [1,2,3,4,5];
var b = a.pop(); //a:[1,2,3,4] b:5 //不用返回的话直接调用就可以了
splice(start,deleteCount,val1,val2,...):从start位置开始删除deleteCount项,并从该位置起插入val1,val2,...
var a = [1,2,3,4,5];
var b = a.splice(2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4]
var b = a.splice(0,1); //同shift
a.splice(0,0,-2,-1); var b = a.length; //同unshift
var b = a.splice(a.length-1,1); //同pop
a.splice(a.length,0,6,7); var b = a.length; //同push
------解决方案--------------------
delete json.marker1
------解决方案--------------------
delete是删除对象的,数组也是对象
------解决方案--------------------