react 深度 循環嵌套對象渲染問題 map


查了一些資料貌似react的循環渲染對象只有map,但map只支持數組對象。

接到后台數據如下


{
  "list": {
    "A": [{
      "image": "http:///b1.jpg",
      "name": "奧迪",
      "id": "1"
    }],
	"B": [{
      "image": "http:///b1.jpg",
      "name": "奔馳",
      "id": "1"
    }] 	
  }
}

需要循環拿到A、B再循環拿到  A、B里邊的數據

異步拿到數據后處理如下:


getbrandInfoFun = async type => {
        try {
            let result = await API.getbrandInfo({
                id: 'wechat'
            });
            let carListArr = [];
            for (let item in result.list) {//async、await中不支持map foreach 所以只能for方法
                let reobj = {};
                reobj[item] = result.list[item];
                carListArr.push(reobj);
            }
            this.setState({
                brandInfoList: carListArr,/* 正面狀態 */
            });
        } catch (err) {
            
          
        }

}

render處理如下:


<div className="carbrand_listbox">
{
	this.state.brandInfoList.map((value, index) => {
		let carListArr=[];
		let carListkey=[];
		for (let item in value) {
			carListkey=item
			carListArr=value[item];
		}
		return (
			<div key={index}>
				<div className="weui-cells__title" >{carListkey}</div>
				{
					carListArr.map((item, numberN) => (
						<div key={numberN} className="weui-cell" data-carbrandid="" ><div className="weui-cell__hd">	<img className="lazy" src={item.image} /></div><div className="weui-cell__bd">	<p >{item.name}}</p></div>
						</div>
					))
				}
			</div>
		)
	})
}
</div>

實現效果:

 

 

感覺不好用但只想到這種方法了,希望以后有更好的方法;

來源:https://blog.csdn.net/qq_16591861/article/details/86527336


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM