英文:
How to filter objects based on whether an array field contains a value?
问题
I have a React app, and I want to filter objects that are in a particular 'category'. On the category object, there is a 'links' field that is an array of Id's. I'm able to get that array and put it into catLinks
, below. The objects that I want to filter through are in the json
object.
What is confusing me is that the filter I am using does return true when I would expect it (as seen in the console log), but then nothing is returned in the filtered
object.
console.log('catLinks: ', catLinks)
// returns 'catLinks: [649d0bae47fa0b4db69f2cf6]';
const filtered = json.filter((l) => {
catLinks.forEach((catLink) => {
if(catLink == l._id) {
console.log(l._id, 'is true')
// returns '649d0bae47fa0b4db69f2cf6 is true'
return true
}
console.log(l._id, 'is false')
return false
})
}).length > 0
console.log('filtered \n', filtered);
// returns 'filtered
// false'
Can somebody help me figure out what is wrong here?
I have tried using includes() as well as the method above, and can not get it to work.
英文:
I have a React app, and I want to filter objects that are in a particular 'category'. On the category object, there is a 'links' field that is an array of Id's. I'm able to get that array and put it into catLinks
, below. The objects that I want to filter through are in the json
object.
What is confusing me is that the filter I am using does return true when I would expect it (as seen in the console log), but then nothing is returned in the filtered
object.
console.log('catLinks: ', catLinks)
// returns 'catLinks: [649d0bae47fa0b4db69f2cf6]'
const filtered = json.filter((l) => {
catLinks.forEach((catLink) => {
if(catLink == l._id) {
console.log(l._id, 'is true')
// returns '649d0bae47fa0b4db69f2cf6 is true'
return true
}
console.log(l._id, 'is false')
return false
})
}).length > 0
console.log('filtered \n', filtered);
// returns 'filtered
// false'
Can somebody help me figure out what is wrong here?
I have tried using includes() as well as the method above, and can not get it to work.
答案1
得分: 1
On javascript, if you want to filter an array you can simply use filter
:
const json = [{_id: "649d0bae47fa0b4db69f2cf6"}, {_id: "649d0bae47fa0b4db69f2cf9"}]
const catLinks = ["649d0bae47fa0b4db69f2cf6", "649d0bae47fa0b4db69f2cf7", "649d0bae47fa0b4db69f2cf8"]
const filtered = json.filter(x => catLinks.includes(x._id))
console.log(filtered)
to get:
[{_id: "649d0bae47fa0b4db69f2cf6"}]
But if json
is a result of a mongodb query, you can get this directly from the query using $in
...
For example:
db.collection.find({
_id: {
$in: [
"649d0bae47fa0b4db69f2cf6",
"649d0bae47fa0b4db69f2cf7",
"649d0bae47fa0b4db69f2cf8"
]
}
})
See how it works on the playground example
*updated to use includes
instead of indexOf
, according to a remark by @epascarello
英文:
On javascript, if you want to filter an array you can simply use filter
:
const json = [{_id: '649d0bae47fa0b4db69f2cf6'}, {_id: '649d0bae47fa0b4db69f2cf9'}]
const catLinks = ['649d0bae47fa0b4db69f2cf6', '649d0bae47fa0b4db69f2cf7', '649d0bae47fa0b4db69f2cf8']
const filtered = json.filter(x => catLinks.includes(x._id))
console.log(filtered)
to get:
[{_id: "649d0bae47fa0b4db69f2cf6"}]
But if json
is a result of a mongodb query, you can get this directly from the query using $in
...
For example:
db.collection.find({
_id: {
$in: [
"649d0bae47fa0b4db69f2cf6",
"649d0bae47fa0b4db69f2cf7",
"649d0bae47fa0b4db69f2cf8"
]
}
})
See how it works on the playground example
*updated to use includes
instead of indexOf
, according to a remark by @epascarello
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论