基于其他数组过滤对象上的数组(Filter an array on objects based on an other array)

编程入门 行业动态 更新时间:2024-10-27 04:33:44
基于其他数组过滤对象上的数组(Filter an array on objects based on an other array)

我是这样的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); 
  
 

I am an arry like this

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 }] }];

And I want to filter subCategory from this array based on following array.

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

So for I have done following, but it is not filtering

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

最满意答案

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

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

演示:

You can use .filter to pull what you need:

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

Demo:

更多推荐

本文发布于:2023-07-23 02:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   Filter   based   objects

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!