var arr = ['a', 'a', 'b', 'c', 'd', 'a', 'a', 'e', 'g', 'a', 'f'];數組
arr.forEach(function(key, index) {
key === 'a' ? arr.splice(index, 1) : '';
})spa
//["a", "b", "c", "d", "a", "e", "g", "f"]3d
arr.forEach(function(key, index) {
key === 'a' ? delete arr[index] : '';
})blog
//["b", "c", "d", "e", "g", "f"]隊列
var newArr = arr.filter(function(key) {
return key !== 'a'
})io
//["b", "c", "d", "e", "g", "f"]console
需求:1 咱們須要刪除掉checked爲"icon-chosen"的全部父項,以及父項中的子項orderDetails。function
2 父項中有多個子項,但若是有一個子項checked是"icon-choose",不是"icon-chosen",那麼父項也須要保留下來不被刪除。List
//上面的數據保存在productList中,咱們看到只有數組中第一個父元素的第二個子元素checked是"icon-choose"循環
var newProductList = [];
productList.forEach(function(key) {
//首先篩選父項"icon-choose",取出咱們須要的父項
if (key.checked == "icon-choose") {
//篩選子項"icon-choose",取出咱們須要的子項
var newOrderDetails = key.orderDetails.filter(function(keys) {
return keys.checked == "icon-choose"
});
//將符合條件的子項賦值給被篩選的子項
key.orderDetails = newOrderDetails;
//將篩選和改變的父項放入新的數組
newProductList.push(key);
}
})
console.info(newProductList);