js json中如何删除指定元素

JavaScript037

js json中如何删除指定元素,第1张

删除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)

}

}

var str=[{"id":"111","name":"张","age":"24","text":15},{"id":"222","name":"三","age":"25","text":15},{"id":"333","name":"张三","age":"26","text":15}]

var s=[]

var l={}

for (var i=0i<str.lengthi++)

{ for(var a in str[i])

{

if(a!="text")

{

l[a]=str[i][a]

}

}

s[i]=l

}

通过复制数组来实现,当为text时则不复制,最后可实现删除text元素功能

满意请采纳