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

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM