vue.js 如何使用字符串数组筛选嵌套对象

fd3cxomn  于 2022-11-25  发布在  Vue.js
关注(0)|答案(4)|浏览(198)

第一个
结果必须是检查数组中所有字符串的对象

{
    name: "A",
    color: "Blue",
    size: {
        size1: 1,
        size2: 2,
        size3: 3,
    },
},

研究必须被接受,无论其价值如何

s5a0g9ez

s5a0g9ez1#

你可以通过递归或者显式迭代地使用堆栈来解决嵌套问题。下面是一个递归的解决方案:

function getFiltered(obj, filters, found = null) {
    let outermostCall = (found === null);
    if (outermostCall) { //outermost call
        found = [];
        for (let index = 0; index < filters.length; index++) {
            found[index] = false;
        }
    }
    for (let key in obj) {
        if (typeof obj[key] === 'object') {
            let tempFound = getFiltered(obj[key], filters, found);
            for (let index = 0; index < found.length; index++) {
                if (tempFound[index]) found[index] = true;
            }
        } else {
            let foundIndex = -1;
            for (let index = 0; index < filters.length; index++) {
                if (filters[index] == obj[key]) {
                    foundIndex = index;
                    index = filters.length;
                }
            }
            if (foundIndex >= 0) {
                found[foundIndex] = true;
            }
        }
    }
    if (outermostCall) {
        return !found.filter(item => !item).length;
    }
    return found;
}

function getAllFiltered(array, filters) {
    let output = [];
    for (let obj of array) {
        if (getFiltered(obj, filters)) output.push(obj);
    }
    return output;
}

let products = [
    {
        name: "A",
        color: "Blue",
        size: {
            size1: 1,
            size2: 2,
            size3: 3,
        },
    },
    {
        name: "B",
        color: "Blue",
        size: {
            size1: 5,
            size2: 19,
            size3: 22,
        },
    },
    { name: "C", color: "Black", size: 70 },
    { name: "D", color: "Green", size: 50 },
];

let filters = ['Blue','2']; 

console.log(getAllFiltered(products, filters));
xtfmy6hx

xtfmy6hx2#

您可以对任何搜索值进行闭包,并检查它们是否都在要过滤的对象或嵌套对象中。
第一个

ulydmbyx

ulydmbyx3#

您可以使用Array.every来检查对象中是否存在所有的滤镜(如果这就是您的意思)。

const products = [
  { name: "A", color: "Blue", size: { size1:1, size2:2, size3:3 } },
  { name: "B", color: "Blue", size: { size1:5, size2:19, size3:22 } },
  { name: "C", color: "Black", size: 70 },
  { name: "D", color: "Green", size: 50 },
];

const filters = ['Blue','2'];

const filtered = products.filter(product => {
  return Object.values(product).every(value => {
    return filters.includes(value);
  });
});

console.log(filtered);
odopli94

odopli944#

函数strings返回对象中的所有嵌套字符串,将任何数字转换为字符串。然后,我们只过滤任何产品,其中字符串列表包括所有必要的匹配。

const products = [{"name":"A","color":"Blue","size":{"size1":1,"size2":2,"size3":3}},{"name":"B","color":"Blue","size":{"size1":5,"size2":19,"size3":22}},{"name":"C","color":"Black","size":70},{"name":"D","color":"Green","size":50}];
const filters = ['Blue','2'];
const subsetMatch=(a,s)=>s.every(i=>a.includes(i));
const strings=i=>typeof i==='object'?Object.values(i).flatMap(strings):i+'';
console.log(products.filter(i=>subsetMatch(strings(i),filters)));

相关问题