js删除json中指定的元素

删除json下指定的元素
var obj = {‘id’:1, ‘name’:2};
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

猜你喜欢

转载自blog.csdn.net/zhangyongbink/article/details/64444159
今日推荐