【问题标题】:How can I map through an object in react js?如何在 React js 中映射对象?
【发布时间】:2023-01-07 21:16:59
【问题描述】:

Here is the response from the server

如何从响应中获取 ResponseText 中的“消息”。

{"errors":[{"value":"","msg":"无效值","param":"name","location":"body"},{"value":"","msg ":"无效值","param":"description","location":"body"},{"value":"","msg":"无效值","param":"catid","位置":"正文"}]}

【问题讨论】:

  • 试试这个:response.errors.map(error => error.msg)

标签: reactjs object axios


【解决方案1】:

这是您的解决方案。

const Example = () => {
  const errors = {
    "errors": [{
      "value": "",
      "msg": "Invalid value",
      "param": "name",
      "location": "body"
    }, {
      "value": "",
      "msg": "Invalid value",
      "param": "description",
      "location": "body"
    }, {
      "value": "",
      "msg": "Invalid value",
      "param": "catid",
      "location": "body"
    }]
  }



  return ( < div >
  
 {errors["errors"].map((item, id) => (

<h1 key={id} >  {item.msg} </h1>
 ))}




    <
    /div>
  );
};

// Render it
ReactDOM.createRoot(
  document.getElementById("root")
).render( <
  Example title = "Example using Hooks:" / >
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js"></script>

【讨论】:

    猜你喜欢
    • 2022-08-04
    • 2019-04-10
    • 2018-08-14
    • 2021-07-21
    • 2017-04-22
    • 2019-03-10
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多