gpt4 book ai didi

javascript - 如何使用对象数组过滤数组对象

转载 作者:行者123 更新时间:2023-12-02 21:35:21 24 4
gpt4 key购买 nike

我有一个像这样的过滤组js对象:

 let filter = {
generalFilter:[
{key: "connected", value: true},
{key: "connected", value: false}
],
locationFilter:[
{key: "deviceLocation", value: "def"},
{key: "deviceLocation", value: "abc"}
],
paymentMethodsFilter:[
{key: "devicePaymentMethods", value: "ab"}
]

}

主数组如下:

const devices = [{
deviceLocation: {
label: "abc",
value: "abc"
},
deviceName: "test7",
devicePaymentMethods: [{
label: "ab",
value: "ab"
}, {
label: "cd",
value: "cd"
}, {
label: "ef",
value: "ef"
}],
deviceType: "iPad",
id: "001",
connected: true,
enabled: true,
},
{
deviceLocation: {
label: "def",
value: "def"
},
deviceName: "test4",
devicePaymentMethods: [{
label: "ab",
value: "ab"
}, {
label: "cd",
value: "cd"
}],
deviceType: "iPad",
id: "004",
connected: false,
enabled: false,
}
];

如何使用过滤器对象过滤此设备数组?这是我尝试过的,但它不起作用

devices.filter((device) => {
let shouldKeep = new Array(3)
shouldKeep.fill(0) //fills the array with 0, meaning no filter groups has passed yet
for(let filterGroupIndex in filter) {// Loop through each filter group in filter
let filterGroup = filter[filterGroupIndex]
for(let filterObject in filterGroup) { //Loop through
if(filterGroup[filterObject].key in device && device[filterGroup[filterObject].key] === filterGroup[filterObject].value) {
shouldKeep[filterGroupIndex] = 1 //Set current filterGroup to 1, meaning it has passed the filter
break; // At least one value in that filter group is true, skip to next filter group
}
}
}

if(shouldKeep.reduce((a,b) => a + b, 0) >= filter.length) {
return true;
}
return false
})

有时,此过滤器对象可以为空,当其为空时,它应该返回没有过滤器主数组的完整数组。做到这一点的最佳方法是什么?我不确定这一点也许还有另一种解决方案。请帮助我

默认的过滤对象是这样的

let filter = {
generalFilter:[],
locationFilter:[],
paymentMethodsFilter:[]

}

用户可以将对象添加到我正在使用 react 钩子(Hook)的过滤器数组中例如

 let filter = {
generalFilter:[
{key: "connected", value: true},
{key: "connected", value: false}
],
locationFilter:[],
paymentMethodsFilter:[]}

然后我们必须检查连接:假和连接:真匹配主数组中的数据(如果主数组中的数据已连接:true,那么它将显示,如果数据已连接:假,它将显示)

对于这种类型的过滤器

let filter = {
generalFilter:[
{key: "connected", value: true},
{key: "connected", value: false}
],
locationFilter:[
{key: "deviceLocation", value: "def"},
{key: "deviceLocation", value: "abc"}
],
paymentMethodsFilter:[
{key: "devicePaymentMethods", value: "ab"}
]

}

结果应该是

result =[{
deviceLocation: {
label: "abc",
value: "abc"
},
deviceName: "test7",
devicePaymentMethods: [{
label: "ab",
value: "ab"
}, {
label: "cd",
value: "cd"
}, {
label: "ef",
value: "ef"
}],
deviceType: "iPad",
id: "001",
connected: true,
enabled: true,
},
{
deviceLocation: {
label: "def",
value: "def"
},
deviceName: "test4",
devicePaymentMethods: [{
label: "ab",
value: "ab"
}, {
label: "cd",
value: "cd"
}],
deviceType: "iPad",
id: "004",
connected: false,
enabled: false,
}
];

因为你可以在过滤器中看到connected:true和connected:false以及带有def和abc的deviceLocation以及devicePaymentMethods是ab

这意味着我希望所有设备的位置 abc 和 def 以及 paymetmethod ab 的连接为 true 和连接为 false

最佳答案

查看评论以获取解释。

const filter = {
generalFilter: [
{key: "connected", value: true},
{key: "connected", value: false}
],
locationFilter: [
{key: "deviceLocation", value: "def"},
{key: "deviceLocation", value: "abc"}
],
paymentMethodsFilter: [
{key: "devicePaymentMethods", value: "ab"}
]
};

const parsedFilter = {};

/**
converting filter into a hashmap with a filter as a Key and value as an
array of possible values.

parsedFilter looks like this
{"connected":[true,false],"deviceLocation":["def","abc"],"paymentOption":["ab"]}

now we can easily check if any value is present in the filter.

**/
const filterKeys = Object.keys(filter);

filterKeys.forEach((filterKey) => {
if (Array.isArray(filter[filterKey])) {
filter[filterKey].forEach((filterItem) => {
if (Object.prototype.hasOwnProperty.call(parsedFilter, filterItem.key)) {
parsedFilter[filterItem.key].push(filterItem.value);
} else {
parsedFilter[filterItem.key] = [filterItem.value];
}
});
}
});


const devices = [
{
deviceLocation: {
label: "abc",
value: "abc"
},
deviceName: "test7",
devicePaymentMethods: [
{
label: "ab",
value: "ab"
}, {
label: "cd",
value: "cd"
}, {
label: "ef",
value: "ef"
}],
deviceType: "iPad",
id: "001",
connected: true,
enabled: true,
}
];

const result = [];

/**
Looping through each device and check if that key is present in the
parsedFilter.

if true: check for typeof value for that device key.
if Object: then check if it is present in the array of not for the
givem deviceKey in parsedFilter.
if Array: then loop through each item and check if it is present
in the parsedFilter for the given deviceKey
if Number, string, boolean: Check if is present in the
parsedFilter for the given deviceKey

if false: simply add it to the result.
**/
if (Array.isArray(devices)) {
devices.forEach((device) => {
const keys = Object.keys(device);
const resultDeviceObj = {};

keys.forEach((key) => {
if (Object.prototype.hasOwnProperty.call(parsedFilter, key)) {
if (typeof device[key] === "object" && parsedFilter[key].includes(device[key].value)) {
resultDeviceObj[key] = device[key];
} else if (Array.isArray(device[key])) {
const arrayResult = [];

device[key].forEach((item) => {
if (parsedFilter[key].includes(item.value)) {
arrayResult.push(item);
}
});

resultDeviceObj[key] = arrayResult;
} else if(parsedFilter[key].includes(device[key])) {
resultDeviceObj[key] = device[key];
}
} else {
resultDeviceObj[key] = device[key];
}
});

result.push(resultDeviceObj);
});
}

console.log("result", result);

Edit

const filter = {
generalFilter: [{key: "connected", value: true}],
locationFilter: [{key: "deviceLocation", value: "abcd"}],
paymentMethodsFilter: [{key: "devicePaymentMethods", value: "ab"}]
};

const parsedFilter = {};

const filterKeys = Object.keys(filter);

filterKeys.forEach((filterKey) => {
if (Array.isArray(filter[filterKey])) {
filter[filterKey].forEach((filterItem) => {
if (Object.prototype.hasOwnProperty.call(parsedFilter, filterItem.key)) {
parsedFilter[filterItem.key][filterItem.value] = filterItem.value;
} else {
parsedFilter[filterItem.key] = {
[filterItem.value]: filterItem.value
}
}
});
}
});

//{"connected":{"true": true,"false": "false"},"deviceLocation":{"def": "def","abc": "abc"},"paymentOption":{"ab": "ab"}}
const devices = [{
deviceLocation: {
label: "abc",
value: "abc"
},
deviceName: "test7",
devicePaymentMethods: [{
label: "ab",
value: "ab"
}, {
label: "cd",
value: "cd"
}, {
label: "ef",
value: "ef"
}],
deviceType: "iPad",
id: "001",
connected: true,
enabled: true,
},
{
deviceLocation: {
label: "def",
value: "def"
},
deviceName: "test4",
devicePaymentMethods: [{
label: "ab",
value: "ab"
}, {
label: "cd",
value: "cd"
}],
deviceType: "iPad",
id: "004",
connected: false,
enabled: false,
}
];

const result = [];

const isObject = function (a) {
return a.constructor.toString().indexOf("Object") !== -1;
};

if (Array.isArray(devices)) {
devices.forEach((device) => {
const keys = Object.keys(device);
let isValid = true;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];

if (Object.prototype.hasOwnProperty.call(parsedFilter, key)) {
if (isObject(device[key]) &&
!Object.prototype.hasOwnProperty.call(parsedFilter[key], device[key].value)) {
isValid = false;
break;
} else if (Array.isArray(device[key])) {
isValid = false;
for (let j = 0; j < device[key].length; j++) {
const item = device[key][j];
if (Object.prototype.hasOwnProperty.call(parsedFilter[key], item.value)) {
isValid = true;
break;
}
}

if (!isValid) {
break;
}
} else if (typeof device[key] === "boolean" &&
!Object.prototype.hasOwnProperty.call(parsedFilter[key], device[key])) {
isValid = false;
break;
}
}
}

if (isValid) {
result.push(device);
}
}
);
}


console.log("result", result)

关于javascript - 如何使用对象数组过滤数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60512084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com