简体   繁体   English

当我在 Javascript 中返回 Object 时未定义

[英]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".我在 VueX 中做吸气剂,当我为另一个 function 返回一个 object 时,我有“未定义”。

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

Basically i have a function like that.基本上我有一个 function 这样的。 When i'm showing obj with console.log(obj), i have an object with elements in here.当我用 console.log(obj) 显示obj时,我有一个 object,其中包含元素。 And basically it's working.基本上它在工作。 But when i'm doing a return and i'm trying to get the obj in another function但是当我return时,我试图在另一个 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:您应该使用find方法而不是map并在您的 getter 中返回找到的项目:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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