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