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

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

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

猜你喜欢

转载自www.cnblogs.com/dalulu/p/9125291.html