【问题标题】:reactjs method keeps returning undefinedreactjs方法不断返回未定义
【发布时间】:2018-10-17 12:44:43
【问题描述】:

我的方法很简单。它遍历一个对象数组并返回参数中具有给定 id 的对象。这里是:

returnValueToDelete(id) {
    this.state.rows.map(function(value) {
      if (value["id"] === id) {
        console.log(value);
        return value;
      }
    });
  }

当我在渲染函数中调用此方法时,实际 id 如下:

console.log(this.returnValueToDelete("IDThatExists"));

它会首先在returnValueToDelete 函数中打印正确的值,因为它被调用,然后打印未定义。谁能解释我为什么以及如何解决这个问题?我在渲染方法中打印了它,因为我想在使用它之前对其进行测试,但它似乎总是会返回undefined,因为我们正在打印返回的内容。非常感谢任何帮助!

编辑

这是我的 this.state.rows:

rows: [
    {
      id: "name",
      numeric: false,
      disablePadding: true,
      label: "Dessert (100g serving)"
    },
    {
      id: "calories",
      numeric: true,
      disablePadding: false,
      label: "Calories"
    },
    {
      id: "fat",
      numeric: true,
      disablePadding: false,
      label: "Fat (g)"
    },
    {
      id: "carbs",
      numeric: true,
      disablePadding: false,
      label: "Carbs (g)"
    },
    {
      id: "protein",
      numeric: true,
      disablePadding: false,
      label: "Protein (g)"
    }
  ]

【问题讨论】:

  • 你的this.state.rows 是什么?还要在else 的情况下提供回报。
  • 在您的 this.state.rows.map... 之前添加一个 return
  • @Arseniy-II this.state.rows 是一个对象列表,其中一个键是“id”。见编辑

标签: javascript reactjs


【解决方案1】:

这是一个在状态中硬编码行的示例。

 this.state = {
   rows: [
     {id:1, name: 'ss'},
     {id:2, name: 'aa'}
   ]
 };

  returnValueToDelete(id) {
    return this.state.rows.filter(value => value.id === id)
  }

  console.log(this.returnValueToDelete(1))

【讨论】:

    【解决方案2】:

    我不知道你为什么用Array.prototype.map()

    如果你想找到匹配元素,你可以使用Array.prototype.find(),像这样

    returnValueToDelete(id) {
      return this.state.rows.find(e => value["id"] === id);
    }
    

    它打印未定义。谁能解释一下为什么以及如何解决这个问题?

    因为你忘记了return 声明。

    【讨论】:

    • 这仍然不起作用。我假设我应该使用value 而不是this.state.rows["id"]
    • 请提供您的this.state.rows 内容
    【解决方案3】:

    它打印未定义。谁能解释一下为什么以及如何解决这个问题?

    您正在正确地创建新数组,但您没有对结果做任何事情。您可以将其保存到变量中,然后将其返回:

    const arr = this.state.rows.map(...
    ...
    return arr;
    

    或者直接返回结果:

    return this.state.rows.map(...
    

    话虽如此,您似乎正在尝试返回原始数组的子集。因此,您应该使用filter() 而不是map()

    returnValueToDelete(id) {
      this.state.rows.filter(function(value) {
        return value["id"] === id
      });
    }
    

    并使用粗箭头语法:

    returnValueToDelete(id) {
      return this.state.rows.filter(value => value["id"] === id);
    }
    

    演示

    const rows = [
      {
        id: "name",
        numeric: false,
        disablePadding: true,
        label: "Dessert (100g serving)"
      },
      {
        id: "calories",
        numeric: true,
        disablePadding: false,
        label: "Calories"
      },
      {
        id: "fat",
        numeric: true,
        disablePadding: false,
        label: "Fat (g)"
      },
      {
        id: "carbs",
        numeric: true,
        disablePadding: false,
        label: "Carbs (g)"
      },
      {
        id: "protein",
        numeric: true,
        disablePadding: false,
        label: "Protein (g)"
      }
    ];
    
    const returnValueToDelete = (id) => {
      return rows.filter(value => value["id"] === id);
    }
    
    console.log(returnValueToDelete("fat"));

    【讨论】:

    • 感谢您的回答。它似乎只适用于粗箭头语法。
    • @tee,对不起。修正了一个错误:P
    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 2020-09-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多