【发布时间】:2022-12-01 22:49:11
【问题描述】:
I can't figure out why my filter/includes is returning an empty array.
I have an array that works as the filter I set.
For example, filterLevels and the array is [100,200] This console.logs out correctly to an array with these vals (integers). The levels array in the data set are also integers and I've double checked. I've console logged out my data set and everything looks good there too.
I'd expect back every overlapping item in both my filtering array and data set. So for the above example I expect the first 3 items from the data set (where there is any match of levels).
console output is an empty array? I must be missing something small?
const filterLevels = [100,200]
const learningMapsData = [{
"name": "Enterprise Networking",
"Technology": "Networking",
"levels": [100, 200]
},
{
"name": "Develop",
"Technology": "Software",
"levels": [100, 200, 300]
},
{
"name": "Test it out",
"Technology": "Testing",
"levels": [200, 300]
},
{
"name": "Rout it",
"Technology": "Routing",
"levels": [300, 400]
}
]
const intersection = learningMapsData.filter(element => filterLevels.includes(element.levels));
console.log("intersection::::::::::::", intersection);
【问题讨论】:
-
Please revise the demo above so it shows your problem.
-
element.levelsis an array. You are checking if any of the elements in[100,200]is equal to an array. Both are numbers so they wont ever be equal to an array. (Though even if the elements infilterLevelswere arrays, then they would still not be equal.)
标签: javascript arrays filter