最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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的回調(diào)函數(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

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

相關(guān)文章

  • react antd checkbox實現(xiàn)全選、多選功能

    react antd checkbox實現(xiàn)全選、多選功能

    目前好像只有table組件有實現(xiàn)表格數(shù)據(jù)的全選功能,如果說對于list,card,collapse等其他組件來說,需要自己結(jié)合checkbox來手動實現(xiàn)全選功能,這篇文章主要介紹了react antd checkbox實現(xiàn)全選、多選功能,需要的朋友可以參考下
    2024-07-07
  • react結(jié)合typescript?封裝組件實例詳解

    react結(jié)合typescript?封裝組件實例詳解

    這篇文章主要為大家介紹了react結(jié)合typescript?封裝組件實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • React State與生命周期詳細(xì)介紹

    React State與生命周期詳細(xì)介紹

    React將組件(component)看成一個狀態(tài)機(State Machines),通過其內(nèi)部自定義的狀態(tài)(State)和生命周期(Lifecycle)實現(xiàn)并與用戶交互,維持組件的不同狀態(tài)
    2022-08-08
  • React如何立即更新DOM

    React如何立即更新DOM

    這篇文章主要介紹了React如何立即更新DOM問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • React動態(tài)更改html標(biāo)簽的實現(xiàn)方式

    React動態(tài)更改html標(biāo)簽的實現(xiàn)方式

    這篇文章主要介紹了React動態(tài)更改html標(biāo)簽的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • react-json-editor-ajrm解析錯誤與解決方案

    react-json-editor-ajrm解析錯誤與解決方案

    由于歷史原因,項目中 JSON 編輯器使用的是 react-json-editor-ajrm,近期遇到一個嚴(yán)重的展示錯誤,傳入編輯器的數(shù)據(jù)與展示的不一致,這是產(chǎn)品和用戶不可接受的,本文給大家介紹了react-json-editor-ajrm解析錯誤與解決方案,需要的朋友可以參考下
    2024-06-06
  • React實現(xiàn)表單提交防抖功能的示例代碼

    React實現(xiàn)表單提交防抖功能的示例代碼

    在 React 應(yīng)用中,防抖(Debounce)技術(shù)可以有效地限制函數(shù)在短時間內(nèi)的頻繁調(diào)用,下面我們就來看看如何使用Lodash庫中的debounce函數(shù)實現(xiàn)React表單提交中實現(xiàn)防抖功能吧
    2024-01-01
  • React使用Context的一些優(yōu)化建議

    React使用Context的一些優(yōu)化建議

    Context?提供了一個無需為每層組件手動添加?props,就能在組件樹間進行數(shù)據(jù)傳遞的方法,本文為大家整理了React使用Context的一些優(yōu)化建議,希望對大家有所幫助
    2024-04-04
  • vscode調(diào)試react?最初的源碼解析

    vscode調(diào)試react?最初的源碼解析

    這篇文章主要介紹了vscode調(diào)試react?最初的源碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友跟隨小編一起看看吧
    2023-11-11
  • React?split實現(xiàn)分割字符串的使用示例

    React?split實現(xiàn)分割字符串的使用示例

    當(dāng)我們需要將一個字符串按照指定的分隔符進行分割成數(shù)組時,我們可以在組件的生命周期方法中使用split方法來實現(xiàn)這個功能,本文就來介紹一下,感興趣的可以了解下
    2023-10-10

最新評論

峨边| 利川市| 东乌珠穆沁旗| 杂多县| 遂昌县| 大庆市| 新丰县| 乐都县| 莱州市| 咸阳市| 鄂尔多斯市| 辽阳县| 祁门县| 中牟县| 林芝县| 原平市| 长海县| 长春市| 安福县| 崇阳县| 沈丘县| 连平县| 台前县| 乐安县| 满洲里市| 通城县| 通化县| 凯里市| 策勒县| 铁力市| 老河口市| 清水河县| 张家口市| 内丘县| 隆子县| 宜州市| 平和县| 漠河县| 陆河县| 山丹县| 巨野县|