使用多个数组

时间:2017-12-09 15:53:58

标签: javascript arrays

如果我有对象数组。

var products = [{
       names: ["hammer", "nail", "rubber duck"],
       colors: ["yellow", "red", "pink"]
    },
    {
       names: ["saw", "tractor", "bowling ball"],
       colors: ["gray", "purple", "green"]
    }]

然后我有过滤值数组的对象:

var filters = {names: ["saw", "bowling ball"], colors:["purple"]}

我需要过滤对象,这样如果我没有过滤器,我会收回所有对象。如果我在"名称"中有任何过滤器值我用匹配的名称返回对象。如果我在"名称"中都有任何过滤值和"颜色"我得到了两个匹配值的对象。

例如:

var filters = {names: ["saw", "bowling ball"], colors:["purple"]}

将返回

[{
names: ["saw", "tractor" "bowling ball"],
colors: ["gray", "purple", "green"]
}]

3 个答案:

答案 0 :(得分:0)

您可以采用嵌套方法,通过过滤给定数组,其中所有项目都包含所需项目的对象并获取其中的键,获取数组并查看该值是否在具有相同键的项目列表中。 / p>

var products = [{ names: ["hammer", "nail", "rubber duck"], colors: ["yellow", "red", "pink"] }, { names: ["saw", "tractor", "bowling ball"], colors: ["gray", "purple", "green"] }],
    filters = { names: ["saw", "bowling ball"], colors: ["purple"] },
    result = products.filter(function (p) {
        return Object.keys(filters).every(function (k) {
            return filters[k].some(function (v) {
                return p[k].indexOf(v) !== -1;
            });
        });
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

    const filterProducts = (filter, products) => {
        let len=products.length,i=0,
            filteredProducts = [];

        for(;i<len;i++) {
            let product = products[i];
            if(filters.names) {
                let names = getMatch(filters.names, product.names);
                if(names) {
                    if(!filteredProducts[i]) filteredProducts[i] = {};
                    filteredProducts[i].names = names;
                };
            }
            if(filters.colors) {
                let colors = getMatch(filters.colors, product.colors);
                if(colors) {
                    if(!filteredProducts[i]) filteredProducts[i] = {};
                    filteredProducts[i].colors = colors;
                };
            }
        }

        if(filteredProducts.length) return filteredProducts;
        return products;
    }

    const getMatch = (filter, values) => {
        let filterLength = filter.length;
        for(let i=0;i<filterLength;i++) {
            if(values.indexOf(filter[i]) !== -1) return values;
        }
    }

    let filtered = filterProducts(filters, products);
    console.log(filtered);

答案 2 :(得分:0)

您可以使用array#filter来遍历您的products数组,在其中,您可以使用name和{{color检查是否存在array#somearray#includes 1}}。

var products = [{ names: ["hammer", "nail", "rubber duck"], colors: ["yellow", "red", "pink"] }, { names: ["saw", "tractor", "bowling ball"],colors: ["gray", "purple", "green"] }],
    filters = {names: ["saw", "bowling ball"], colors:["purple"]};
var result = products.filter(({names, colors}) => {
  return filters.names.some(name => names.includes(name)) && filters.colors.some(color => colors.includes(color));
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }