js删除数组中某一项,splice()

var id = '3'
var tagList = [{"parentTagId":"1","parentTagName":"学校","childTagId":"3","childTagName":"高中"}, {"parentTagId":"1","parentTagName":"学校","childTagId":"4","childTagName":"初中"}] for(var i = 0; i < tagList.length; i++){ if(tagList[i].childTagId === id){ tagList.splice(i,1); } } console.log(JSON.stringify(tagList))

 

 

或者直接是哪对象过来也行,同样的方法:html

 

var obj = { parentTagId : '1', parentTagName:"学校", childTagId: '3', childTagName:"高中" } var tagList = [{"parentTagId":"1","parentTagName":"学校","childTagId":"3","childTagName":"高中"}, {"parentTagId":"1","parentTagName":"学校","childTagId":"4","childTagName":"初中"}] for(var i = 0; i < tagList.length; i++){ if(tagList[i].childTagId === obj.childTagId){ tagList.splice(i,1); } } console.log(JSON.stringify(tagList))
相关文章
相关标签/搜索