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

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

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

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

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

map不能遍歷array數(shù)組,只能遍歷object對(duì)象。

所以如果遇到這樣的問(wèn)題可以采用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}&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ù)組時(shí)map和forEach的區(qū)別

1. map函數(shù)返回一個(gè)新的數(shù)組,在map的回調(diào)函數(shù)里,迭代每一項(xiàng)的時(shí)候也必須有返回值。

2. forEach 沒(méi)有返回值

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 antd checkbox實(shí)現(xiàn)全選、多選功能

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

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

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

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

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

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

    React如何立即更新DOM

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

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

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

    react-json-editor-ajrm解析錯(cuò)誤與解決方案

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

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

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

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

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

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

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

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

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

最新評(píng)論

沙洋县| 台安县| 巴中市| 那坡县| 左权县| 志丹县| 神池县| 无棣县| 开远市| 汉中市| 确山县| 广安市| 大邑县| 盐源县| 乌拉特前旗| 屏东县| 陈巴尔虎旗| 尼勒克县| 罗甸县| 沽源县| 苏州市| 汪清县| 吉木萨尔县| 视频| 文山县| 马山县| 黄浦区| 峨边| 林周县| 昌乐县| 广灵县| 保靖县| 蚌埠市| 纳雍县| 伽师县| 仙游县| 晴隆县| 和静县| 麟游县| 望奎县| 岳阳县|