思路
第一种方法:通过把json中需要的值取出来,重新生成json对象,这种方法比较笨
第二种方法:通过delete 删除属性,这种方法比较常用,在第三方js库中经常能看到,推荐
举例
<script>var s = [{"name":"tank","total":"100"},{"name":"zhang","total":"23"},{"name":"hao","total":"325"}]
for(var k=0k<s.lengthk++){
delete s[k]["name"]
}
</script>
删除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)
}
}