当前位置:   article > 正文

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

js json remove

删除json下指定的元素

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

删除数组中指定元素

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

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

Array.prototype.remove = function(val) { 
    var index = this.indexOf(val); 
    if (index > -1) { 
   		this.splice(index, 1); 
    } 
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/283650
推荐阅读
相关标签
  

闽ICP备14008679号