您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“react中怎么使用forEach或map兩種方式遍歷數(shù)組”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“react中怎么使用forEach或map兩種方式遍歷數(shù)組”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
之前寫代碼,從后臺(tái)提取數(shù)據(jù)并渲染到前臺(tái),由于有多組數(shù)據(jù),用map遍歷會(huì)相對方便一點(diǎn),但是
map不能遍歷array數(shù)組,只能遍歷object對象。
所以如果遇到這樣的問題可以采用forEach試一下
import React,{Component} from 'react'; let list=[ { name:"百度", address:"http://www.baidu.com" }, { name:"google", address:"http://www.google.cn" }, { name:"firefox", address:"https://home.firefoxchina.cn" } ]; class forEach extends Component{ render(){ //定義一個(gè)數(shù)組,將數(shù)據(jù)存入數(shù)組 const elements=[]; list.forEach((item)=>{ elements.push( <div> {item.name} <a>{item.address}</a> <hr/> </div> ) }); return( <div> {elements} </div> ) } } export default forEach;
import React,{Component} from 'react'; let list=[ { name:"百度", address:"http://www.baidu.com" }, { name:"google", address:"http://www.google.cn" }, { name:"firefox", address:"https://home.firefoxchina.cn" } ]; class forEach extends Component{ render(){ return( list.map((item)=> <div> {item.name} <a>{item.address}</a> <hr/> </div> ) ) } } export default forEach;
1. map函數(shù)返回一個(gè)新的數(shù)組,在map的回調(diào)函數(shù)里,迭代每一項(xiàng)的時(shí)候也必須有返回值。
2. forEach 沒有返回值
import React, { Component } from "react" import ListItem from './ListItem' class TodoList extends Component { constructor(props) { super(props); this.state = { inputValue: '', list: ['bb', 'ccc'] }; this.changeInput = this.changeInput.bind(this); } changeInput(e) { this.setState({ inputValue: e.target.value }) } commitInput = () => { const newList = JSON.parse(JSON.stringify(this.state.list)); newList.push(this.state.inputValue); this.setState({ list: newList, inputValue: '' }) } deleteItem = index => { this.state.list.splice(index, 1); this.setState ({ list: this.state.list }) } componentDidMount() { console.log('parent didmount') } render() { console.log('parent render') const elements = [] this.state.list.forEach((item, index) => { elements.push( <ListItem key={index} content={item} index={index} deleteItem={(index) => { this.deleteItem(index) }} /> ) }) { console.log('zzz') } return ( <div> <input type="text" value={this.state.inputValue} onChange={this.changeInput} /> <button onClick={this.commitInput}>提交</button> <ul> { console.log('mmm') } { elements } </ul> </div> ) } } export default TodoList
import React, { Component } from "react" import ListItem from './ListItem' class TodoList extends Component { constructor(props) { super(props); this.state = { inputValue: '', list: ['bb', 'ccc'] }; this.changeInput = this.changeInput.bind(this); } changeInput(e) { this.setState({ inputValue: e.target.value }) } commitInput = () => { const newList = JSON.parse(JSON.stringify(this.state.list)); newList.push(this.state.inputValue); this.setState({ list: newList, inputValue: '' }) } deleteItem = index => { this.state.list.splice(index, 1); this.setState ({ list: this.state.list }) } componentDidMount() { console.log('parent didmount') } render() { console.log('parent render') return ( <div> <input type="text" value={this.state.inputValue} onChange={this.changeInput} /> <button onClick={this.commitInput}>提交</button> <ul> { this.state.list.map((item, index) => { return( <ListItem key={index} content={item} index={index} deleteItem={(index) => { this.deleteItem(index) }} /> ) }) } </ul> </div> ) } } export default TodoList
讀到這里,這篇“react中怎么使用forEach或map兩種方式遍歷數(shù)組”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。