刪除數組中指定的某個對象


1.首先說下字符串與對象之間的轉換:

JSON.stringfy()將對象、數組轉換成字符串;

JSON.parse()將字符串轉成json對象。

2.刪除數組對象中name='已辦結的'對象


var newArr= [{name: "已辦結", value: "10015"}, {name: "已撤銷", value: "276"}, {name: "班組未分配", value: "183"}, {name: "處理中", value: "177"}, {name: "未派單", value: "94"}, {name: "已暫停", value: "9"}] 

方法一:


newArr.forEach((value,index,array)=>{ if(value.name =='已辦結'){ array.splice(value,1) // delete array[index] //方法二 } }) 

方法三:


var arrNew=[]; newArr.forEach((value,index,array)=>{ if(value.name ! =='已辦結'){ arrNew.push(value); } }) 

說明:

Array.forEach(function(value , index , array){ //value為遍歷的當前元素,index為當前索引,array為正在操作的數組

//do something

},thisArg)

2.刪除數組對象中除了name='已辦結的'對象


var newArr=arrNew.filter(function(item){ return item.name.match(/已辦結/) }) 


作者:程序員是粉色的
鏈接:https://www.jianshu.com/p/a51d50b16fb8
來源:簡書


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM