Skip to content

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

Answer

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)
}