如何在jsx react中循环嵌套对象

我正在使用 react 我想在 UI 上显示一些动态内容,但由于我是新手而无法循环数据,所以发现这很难做到


 state={

dashboardManpowerCount:{

        "CurrentMonth": {

          "Total No of employes": 25,

          "Ariving": 10,

          "Exited": 8

        },

        "PreviousMonth": {

          "Total No of employes": 25,

          "Ariving": 10,

          "Exited": 8

        }

      }

           }


class currAndprevMonthcCounts extends Component {

render(){

const {dashboardManpowerCount} = this.state


return(

<div>

<div className="col-6 col-sm-6 col-md-6 col-lg-3 col-xl-3">

                    <div className="row">

                       <div className="col-12 col-sm-12 col-md-6 col-lg-5 col-xl-5">

                       <h6>Previous Month</h6>

                    <h2>395</h2> 

                    </div>

                    <div className="col-12 col-sm-12 col-md-6 col-lg-7 col-xl-7">


                    <span className="badge badge-pill badge-secondary mt-2"

                    >+7 Ariving</span>

                    <br></br>


                    <span className="badge badge-pill badge-secondary mt-3">-3 Exiting</span> 


                    </div>

                </div>

                </div>

                <div className="col-6 col-sm-6 col-md-6 col-lg-3 col-xl-3">

                <div className="row">

                       <div className="col-12 col-sm-12 col-md-6 col-lg-5 col-xl-5">

                    <h6>Previous Month</h6>

                    <h2>395</h2>

                    </div>

                    <div className="col-12 col-sm-12 col-md-6 col-lg-7 col-xl-7">


                    <span className="badge badge-pill badge-secondary mt-2">+5 Ariving</span>

                    <br></br>


                    <span className="badge badge-pill badge-secondary mt-3">-3 Exiting</span> 


                    </div>

                </div>

                </div>

</div>

)

}


}

有两个选项当前月份数据和上个月数据我想循环访问对象并代替我的 jsx 中的静态内容呈现


这就是

http://img.mukewang.com/629c66c300011ad204570092.jpg

人到中年有点甜
浏览 181回答 1
1回答

子衿沉夜

使用object.keys或object.entries循环对象的属性。&nbsp; render() {&nbsp; &nbsp; const { dashboardManpowerCount } = this.state;&nbsp; &nbsp; const dashboardManpowerCountArray = Object.entries(dashboardManpowerCount);&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; {dashboardManpowerCountArray.map(arr => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div key={arr[0]}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>{arr[0]}</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {Object.entries(arr[1]).map(monthArr => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span key={monthArr[0]} style={{ display: "block" }}>{`${&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; monthArr[0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ${monthArr[1]}`}</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; })}&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; }看到这个stackblitz。显然改变你喜欢的样式和标签。更新这是您可以用来显示数据的 jsx -import React from "react";import { render } from "react-dom";import "bootstrap/dist/css/bootstrap.min.css";class Events extends React.Component {&nbsp; state = {&nbsp; &nbsp; dashboardManpowerCount: {&nbsp; &nbsp; &nbsp; CurrentMonth: {&nbsp; &nbsp; &nbsp; &nbsp; "Total No of employes": 25,&nbsp; &nbsp; &nbsp; &nbsp; Ariving: 10,&nbsp; &nbsp; &nbsp; &nbsp; Exited: 8&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; PreviousMonth: {&nbsp; &nbsp; &nbsp; &nbsp; "Total No of employes": 25,&nbsp; &nbsp; &nbsp; &nbsp; Ariving: 10,&nbsp; &nbsp; &nbsp; &nbsp; Exited: 8&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; };&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div className="divParent row container">&nbsp; &nbsp; &nbsp; &nbsp; {Object.entries(this.state.dashboardManpowerCount).map(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ([monthName, monthData]) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div className="col-6 col-sm-6 col-md-6 col-lg-3 col-xl-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div className="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div className="col-12 col-sm-12 col-md-6 col-lg-5 col-xl-5">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h6>{monthName}</h6>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2>{monthData["Total No of employes"]}</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div className="col-12 col-sm-12 col-md-6 col-lg-7 col-xl-7">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span className="badge badge-pill badge-secondary mt-2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {`+${monthData.Ariving} Ariving`}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span className="badge badge-pill badge-secondary mt-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {`-${monthData.Ariving} Exiting`}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; )}&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; }}render(<Events />, document.getElementById("root"));但是这次组件不是完全动态的。如果您的对象的架构将来发生变化,您也必须更改 jsx。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript