基于另一个数组

时间:2015-09-14 14:39:08

标签: javascript

我是arry喜欢这个

var data = [{
        category: "2014-1-1",
        subCategory: [{
            name: "shoe",
            val: 70,
            toolTip: "toolTip-shoe-70",
            drill: false
        }, {
            name: "pant",
            val: 60,
            toolTip: "toolTip-pant-60",
            drill: false
        }, {
            name: "shirt",
            val: 50,
            toolTip: "toolTip-shirt-50",
            drill: false
        }, {
            name: "Tshirt",
            val: 40,
            toolTip: "toolTip-Tshirt-40",
            drill: true
        }, {
            name: "socks",
            val: 30,
            toolTip: "toolTip-socks-30",
            drill: false
        }, {
            name: "watchs",
            val: 20,
            toolTip: "toolTip-watchs-20",
            drill: false
        }, {
            name: "hats",
            val: 10,
            toolTip: "toolTip-hats-10",
            drill: false
        }]
    }, {
        category: "2014-3-2",
        subCategory: [{
            name: "shoe",
            val: 16,
            toolTip: "toolTip-shoe-16",
            drill: false
        }, {
            name: "pant",
            val: 12,
            toolTip: "toolTip-pant-12",
            drill: false
        }, {
            name: "shirt",
            val: 14,
            toolTip: "toolTip-shirt-14",
            drill: false
        }, {
            name: "Tshirt",
            val: 13,
            toolTip: "toolTip-Tshirt-13",
            drill: false
        }, {
            name: "socks",
            val: 22,
            toolTip: "toolTip-socks-22",
            drill: true
        }, {
            name: "watchs",
            val: 31,
            toolTip: "toolTip-watchs-31",
            drill: false
        }, {
            name: "hats",
            val: 21,
            toolTip: "toolTip-hats-21",
            drill: false
        }]
    }, {
        category: "2014-6-3",
        subCategory: [{
            name: "shoe",
            val: 22,
            toolTip: "toolTip-shoe-22",
            drill: false
        }, {
            name: "pant",
            val: 25,
            toolTip: "toolTip-pant-25",
            drill: false
        }, {
            name: "shirt",
            val: 28,
            toolTip: "toolTip-shirt-28",
            drill: false
        }, {
            name: "Tshirt",
            val: 33,
            toolTip: "toolTip-Tshirt-33",
            drill: false
        }, {
            name: "socks",
            val: 25,
            toolTip: "toolTip-socks-25",
            drill: false
        }, {
            name: "watchs",
            val: 40,
            toolTip: "toolTip-watchs-40",
            drill: false
        }, {
            name: "hats",
            val: 38,
            toolTip: "toolTip-hats-38",
            drill: true
        }]
    }, {
        category: "2014-9-3",
        subCategory: [{
            name: "shoe",
            val: 25,
            toolTip: "toolTip-shoe-25",
            drill: false
        }, {
            name: "pant",
            val: 33,
            toolTip: "toolTip-pant-33",
            drill: false
        }, {
            name: "shirt",
            val: 37,
            toolTip: "toolTip-shirt-37",
            drill: false
        }, {
            name: "Tshirt",
            val: 38,
            toolTip: "toolTip-Tshirt-38",
            drill: false
        }, {
            name: "socks",
            val: 32,
            toolTip: "toolTip-socks-32",
            drill: false
        }, {
            name: "watchs",
            val: 28,
            toolTip: "toolTip-watchs-28",
            drill: true
        }, {
            name: "hats",
            val: 41,
            toolTip: "toolTip-hats-41",
            drill: false
        }]
    }, {
        category: "2014-12-4",
        subCategory: [{
            name: "shoe",
            val: 22,
            toolTip: "toolTip-shoe-22",
            drill: false
        }, {
            name: "pant",
            val: 41,
            toolTip: "toolTip-pant-41",
            drill: false
        }, {
            name: "shirt",
            val: 33,
            toolTip: "toolTip-shirt-33",
            drill: false
        }, {
            name: "Tshirt",
            val: 22,
            toolTip: "toolTip-Tshirt-22",
            drill: false
        }, {
            name: "socks",
            val: 11,
            toolTip: "toolTip-socks-11",
            drill: false
        }, {
            name: "watchs",
            val: 39,
            toolTip: "toolTip-watchs-39",
            drill: false
        }, {
            name: "hats",
            val: 32,
            toolTip: "toolTip-hats-32",
            drill: false
        }]
    }, {
        category: "2015-3-5",
        subCategory: [{
            name: "shoe",
            val: 33,
            toolTip: "toolTip-shoe-33",
            drill: false
        }, {
            name: "pant",
            val: 51,
            toolTip: "toolTip-pant-51",
            drill: false
        }, {
            name: "shirt",
            val: 43,
            toolTip: "toolTip-shirt-43",
            drill: false
        }, {
            name: "Tshirt",
            val: 43,
            toolTip: "toolTip-Tshirt-43",
            drill: false
        }, {
            name: "socks",
            val: 46,
            toolTip: "toolTip-socks-46",
            drill: false
        }, {
            name: "watchs",
            val: 47,
            toolTip: "toolTip-watchs-47",
            drill: false
        }, {
            name: "hats",
            val: 49,
            toolTip: "toolTip-hats-49",
            drill: false
        }]
    }];  

我想基于以下数组从此数组中过滤subCategory

var arr = ["hats", "watchs"];  

因为我已经完成了以下操作,但它没有过滤



var data = [{
        category: "2014-1-1",
        subCategory: [{
            name: "shoe",
            val: 70,
            toolTip: "toolTip-shoe-70",
            drill: false
        }, {
            name: "pant",
            val: 60,
            toolTip: "toolTip-pant-60",
            drill: false
        }, {
            name: "shirt",
            val: 50,
            toolTip: "toolTip-shirt-50",
            drill: false
        }, {
            name: "Tshirt",
            val: 40,
            toolTip: "toolTip-Tshirt-40",
            drill: true
        }, {
            name: "socks",
            val: 30,
            toolTip: "toolTip-socks-30",
            drill: false
        }, {
            name: "watchs",
            val: 20,
            toolTip: "toolTip-watchs-20",
            drill: false
        }, {
            name: "hats",
            val: 10,
            toolTip: "toolTip-hats-10",
            drill: false
        }]
    }, {
        category: "2014-3-2",
        subCategory: [{
            name: "shoe",
            val: 16,
            toolTip: "toolTip-shoe-16",
            drill: false
        }, {
            name: "pant",
            val: 12,
            toolTip: "toolTip-pant-12",
            drill: false
        }, {
            name: "shirt",
            val: 14,
            toolTip: "toolTip-shirt-14",
            drill: false
        }, {
            name: "Tshirt",
            val: 13,
            toolTip: "toolTip-Tshirt-13",
            drill: false
        }, {
            name: "socks",
            val: 22,
            toolTip: "toolTip-socks-22",
            drill: true
        }, {
            name: "watchs",
            val: 31,
            toolTip: "toolTip-watchs-31",
            drill: false
        }, {
            name: "hats",
            val: 21,
            toolTip: "toolTip-hats-21",
            drill: false
        }]
    }, {
        category: "2014-6-3",
        subCategory: [{
            name: "shoe",
            val: 22,
            toolTip: "toolTip-shoe-22",
            drill: false
        }, {
            name: "pant",
            val: 25,
            toolTip: "toolTip-pant-25",
            drill: false
        }, {
            name: "shirt",
            val: 28,
            toolTip: "toolTip-shirt-28",
            drill: false
        }, {
            name: "Tshirt",
            val: 33,
            toolTip: "toolTip-Tshirt-33",
            drill: false
        }, {
            name: "socks",
            val: 25,
            toolTip: "toolTip-socks-25",
            drill: false
        }, {
            name: "watchs",
            val: 40,
            toolTip: "toolTip-watchs-40",
            drill: false
        }, {
            name: "hats",
            val: 38,
            toolTip: "toolTip-hats-38",
            drill: true
        }]
    }, {
        category: "2014-9-3",
        subCategory: [{
            name: "shoe",
            val: 25,
            toolTip: "toolTip-shoe-25",
            drill: false
        }, {
            name: "pant",
            val: 33,
            toolTip: "toolTip-pant-33",
            drill: false
        }, {
            name: "shirt",
            val: 37,
            toolTip: "toolTip-shirt-37",
            drill: false
        }, {
            name: "Tshirt",
            val: 38,
            toolTip: "toolTip-Tshirt-38",
            drill: false
        }, {
            name: "socks",
            val: 32,
            toolTip: "toolTip-socks-32",
            drill: false
        }, {
            name: "watchs",
            val: 28,
            toolTip: "toolTip-watchs-28",
            drill: true
        }, {
            name: "hats",
            val: 41,
            toolTip: "toolTip-hats-41",
            drill: false
        }]
    }, {
        category: "2014-12-4",
        subCategory: [{
            name: "shoe",
            val: 22,
            toolTip: "toolTip-shoe-22",
            drill: false
        }, {
            name: "pant",
            val: 41,
            toolTip: "toolTip-pant-41",
            drill: false
        }, {
            name: "shirt",
            val: 33,
            toolTip: "toolTip-shirt-33",
            drill: false
        }, {
            name: "Tshirt",
            val: 22,
            toolTip: "toolTip-Tshirt-22",
            drill: false
        }, {
            name: "socks",
            val: 11,
            toolTip: "toolTip-socks-11",
            drill: false
        }, {
            name: "watchs",
            val: 39,
            toolTip: "toolTip-watchs-39",
            drill: false
        }, {
            name: "hats",
            val: 32,
            toolTip: "toolTip-hats-32",
            drill: false
        }]
    }, {
        category: "2015-3-5",
        subCategory: [{
            name: "shoe",
            val: 33,
            toolTip: "toolTip-shoe-33",
            drill: false
        }, {
            name: "pant",
            val: 51,
            toolTip: "toolTip-pant-51",
            drill: false
        }, {
            name: "shirt",
            val: 43,
            toolTip: "toolTip-shirt-43",
            drill: false
        }, {
            name: "Tshirt",
            val: 43,
            toolTip: "toolTip-Tshirt-43",
            drill: false
        }, {
            name: "socks",
            val: 46,
            toolTip: "toolTip-socks-46",
            drill: false
        }, {
            name: "watchs",
            val: 47,
            toolTip: "toolTip-watchs-47",
            drill: false
        }, {
            name: "hats",
            val: 49,
            toolTip: "toolTip-hats-49",
            drill: false
        }]
    }];
    var arr = ["hats", "watchs"];

    var abc = data.filter(function (category) {
        var obj = {};
        var arr1 = [];
        category.subCategory.filter(function (d) {

            for (var i = 0; i < arr.length; i++) {
                if (arr[i] != d.name) {
                    arr1.push(d);
                }
            }
        });
        obj["category"] = category.category;
        obj["subCategory"] = arr1;
        return obj;
    });
    console.log(abc);
    console.log(data);
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您可以使用.filter来提取所需内容:

var filteredData = data.filter(function(topLevel) {
    topLevel.subCategory = topLevel.subCategory.filter(function(bottomLevel) {
        return arr.indexOf(bottomLevel.name) < 0;
    })

    return topLevel.subCategory.length;
});

Demo:

答案 1 :(得分:0)

First example of doing it:

var result = [].concat.apply([], data.map(function (child) {
    return child.subCategory;
})).filter(function (item) {
    return arr.indexOf(item.name) >= 0;
});

Second example:

var result2 = data.reduce(function(prev, current) {
    return prev.concat(current.subCategory);
}, []).reduce(function (prev, current) {
    if (arr.indexOf(current.name) >= 0) {
        prev.push(current);
    }
    return prev;
}, []);

Demo: https://jsfiddle.net/63s2bqkf/1/

相关问题