简体   繁体   中英

Undefined when i am returning an Object in Javascript

I'm doing a getter in VueX, and when i'm returning an object for another function, i have "undefined".

  getId: (state) => (LotofID, id) => {
    LotofID.points.map(obj => {
      if (obj.id === id)
        return (obj);
   })

Basically i have a function like that. When i'm showing obj with console.log(obj), i have an object with elements in here. And basically it's working. But when i'm doing a return and i'm trying to get the obj in another function

var test = []
selectedRowKeys.map(obj => {
    test.push(this.$store.getters.getId(LotofID, obj))
  })
  console.log(test)

I have a "undefined" in my variable. Anyone have an idea of where the problem can be

You should use find method instead of map and return the found item inside your getter:

 getId: (state) => (LotofID, id) => {
    return LotofID.points.find(obj => obj.id === id)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM