如何根据数组字段是否包含一个值来过滤对象?

huangapple go评论82阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月29日 21:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定