react中使用forEach或map兩種方式遍歷數(shù)組
使用forEach或map兩種方式遍歷數(shù)組
之前寫代碼,從后臺(tái)提取數(shù)據(jù)并渲染到前臺(tái),由于有多組數(shù)據(jù),用map遍歷會(huì)相對方便一點(diǎn),但是
map不能遍歷array數(shù)組,只能遍歷object對象。
所以如果遇到這樣的問題可以采用forEach試一下
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;
map
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;
循環(huán)遍歷數(shù)組時(shí)map和forEach的區(qū)別
1. map函數(shù)返回一個(gè)新的數(shù)組,在map的回調(diào)函數(shù)里,迭代每一項(xiàng)的時(shí)候也必須有返回值。
2. forEach 沒有返回值
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
map 情況
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
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
react?路由權(quán)限動(dòng)態(tài)菜單方案配置react-router-auth-plus
這篇文章主要為大家介紹了react路由權(quán)限動(dòng)態(tài)菜單方案react-router-auth-plus傻瓜式配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08ahooks控制時(shí)機(jī)的hook實(shí)現(xiàn)方法
這篇文章主要為大家介紹了ahooks控制時(shí)機(jī)的hook實(shí)現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07react中history(push,go,replace)切換路由方法的區(qū)別及說明
這篇文章主要介紹了react中history(push,go,replace)切換路由方法的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10react koa rematch 如何打造一套服務(wù)端渲染架子
這篇文章主要介紹了react koa rematch 如何打造一套服務(wù)端渲染架子,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06react+antd.3x實(shí)現(xiàn)ip輸入框
這篇文章主要為大家詳細(xì)介紹了react+antd.3x實(shí)現(xiàn)ip輸入框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10詳解react中useCallback內(nèi)部是如何實(shí)現(xiàn)的
前幾天有人在問在useCallback函數(shù)如果第二個(gè)參數(shù)為空數(shù)組, 為什么拿不到最新的state值,那么這一章就來分析一下useCallback內(nèi)部是如何實(shí)現(xiàn)的,感興趣的小伙伴跟著小編一起來學(xué)習(xí)吧2023-07-07