欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react中使用forEach或map兩種方式遍歷數(shù)組

 更新時間:2022年09月07日 16:11:08   作者:a little peanut  
這篇文章主要介紹了react中使用forEach或map兩種方式遍歷數(shù)組,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

使用forEach或map兩種方式遍歷數(shù)組

之前寫代碼,從后臺提取數(shù)據(jù)并渲染到前臺,由于有多組數(shù)據(jù),用map遍歷會相對方便一點,但是

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(){
    //定義一個數(shù)組,將數(shù)據(jù)存入數(shù)組
    const elements=[];
    list.forEach((item)=>{
      elements.push(
        <div>
          {item.name}&nbsp;
          <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}&nbsp;
          <a>{item.address}</a>
          <hr/>
        </div>
      )
    )
  }
}
export default forEach;

在這里插入圖片描述

循環(huán)遍歷數(shù)組時map和forEach的區(qū)別

1. map函數(shù)返回一個新的數(shù)組,在map的回調函數(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

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關文章

  • React 路由react-router-dom示例詳解

    React 路由react-router-dom示例詳解

    一個路由就是一個映射關系(key:value),key為路徑, value可能是function或component,本文給大家介紹React 路由react-router-dom詳解,感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • Ant Design中使用css切換的問題及解決

    Ant Design中使用css切換的問題及解決

    這篇文章主要介紹了Ant Design中使用css切換的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • react?路由權限動態(tài)菜單方案配置react-router-auth-plus

    react?路由權限動態(tài)菜單方案配置react-router-auth-plus

    這篇文章主要為大家介紹了react路由權限動態(tài)菜單方案react-router-auth-plus傻瓜式配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • ahooks控制時機的hook實現(xiàn)方法

    ahooks控制時機的hook實現(xiàn)方法

    這篇文章主要為大家介紹了ahooks控制時機的hook實現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • react中history(push,go,replace)切換路由方法的區(qū)別及說明

    react中history(push,go,replace)切換路由方法的區(qū)別及說明

    這篇文章主要介紹了react中history(push,go,replace)切換路由方法的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • react koa rematch 如何打造一套服務端渲染架子

    react koa rematch 如何打造一套服務端渲染架子

    這篇文章主要介紹了react koa rematch 如何打造一套服務端渲染架子,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • react+antd.3x實現(xiàn)ip輸入框

    react+antd.3x實現(xiàn)ip輸入框

    這篇文章主要為大家詳細介紹了react+antd.3x實現(xiàn)ip輸入框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 詳解React 代碼共享最佳實踐方式

    詳解React 代碼共享最佳實踐方式

    這篇文章主要介紹了React 代碼共享最佳實踐方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-05-05
  • Remix中mdx?table不支持表格解決

    Remix中mdx?table不支持表格解決

    這篇文章主要為大家介紹了Remix中mdx?table不支持表格問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 詳解react中useCallback內部是如何實現(xiàn)的

    詳解react中useCallback內部是如何實現(xiàn)的

    前幾天有人在問在useCallback函數(shù)如果第二個參數(shù)為空數(shù)組, 為什么拿不到最新的state值,那么這一章就來分析一下useCallback內部是如何實現(xiàn)的,感興趣的小伙伴跟著小編一起來學習吧
    2023-07-07

最新評論