js怎么删除json中指定的元素

删除json下指定的元素

var obj = {'id':1, 'name':'小明'}; 
delete obj.id; 
delete obj[id]; 
console.log(obj); // {'name':2}

删除数组中指定元素

var objArray = ['1','2','3','4']; 
objArray.remove('1'); 
console.log(objArray); // ['2','3','4'] 

/*定义js数组删除元素/

Array.prototype.remove = function(val) { 
    var index = this.indexOf(val); 
    if (index > -1) { 
   		this.splice(index, 1); 
    } 
};

jquery判断数组中是否含有某个元素
$.inArray('1',objArray) //返回’1’的索引,如果不存在则返回-1

发布了35 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39024950/article/details/88947707