您现在的位置是:网站首页> 编程资料编程资料
react中使用forEach或map两种方式遍历数组_React_
2023-05-24
368人已围观
简介 react中使用forEach或map两种方式遍历数组_React_
使用forEach或map两种方式遍历数组
之前写代码,从后台提取数据并渲染到前台,由于有多组数据,用map遍历会相对方便一点,但是
map不能遍历array数组,只能遍历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(){ //定义一个数组,将数据存入数组 const elements=[]; list.forEach((item)=>{ elements.push( {item.name} {item.address}
) }); return( {elements} ) } } 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)=> {item.name} {item.address}
) ) } } export default forEach; 
循环遍历数组时map和forEach的区别
1. map函数返回一个新的数组,在map的回调函数里,迭代每一项的时候也必须有返回值。
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( { this.deleteItem(index) }} /> ) }) { console.log('zzz') } return ( { console.log('mmm') } { elements }
) } } 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 ( { this.state.list.map((item, index) => { return( { this.deleteItem(index) }} /> ) }) }
) } } export default TodoList以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关内容
- React Native可复用 UI分离布局组件和状态组件技巧_React_
- React组件实例三大属性state props refs使用详解_React_
- javascript手机验证、邮箱验证、密码验证的正则表达式简单封装实例_javascript技巧_
- 小程序实现页面跳转与数据传递方案_javascript技巧_
- 关于JavaScript中的数组方法和循环_javascript技巧_
- 使用react在修改state中的数组和对象数据的时候(setState)_React_
- vue如何修改data中数据问题_vue.js_
- vue实现动态给data函数中的属性赋值_vue.js_
- Vue openLayers实现图层数据切换与加载流程详解_vue.js_
- vue中data数据之间如何赋值问题_vue.js_
