简体   繁体   English

reactjs如何在数组中插入数据?

[英]How to insert data in array in react js?

I'm using react.js and I have class base component.我正在使用 react.js,并且我有 class 基本组件。 Now the scenario is.现在的情况是。 I have one array with the name of partitions.我有一个带有分区名称的数组。

partitions=["P1","P2","P3"]

and I have another array with the name dayDetails which is null at the start.我有另一个名为dayDetails的数组,开头是 null。

When my components mounts.当我的组件安装时。 I map partitions array and add an object in dayDetail array.我 map 分区数组并在 dayDetail 数组中添加一个 object。 Number of elements in partition array will be the number of objects put in the dayDetail array.分区数组中的元素数将是放入 dayDetail 数组中的对象数。 Now the problem is only One object is putting in the dayDetail but it suppose to add three because number of partitions are 3.现在的问题是只有一个 object 正在放入 dayDetail 但它假设添加三个,因为分区数是 3。

here is my code below.下面是我的代码。

import React from "react";

class Step2 extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
        partitions:["P1","P2","P3"],
        dayDetails:[],

    };
  }


  componentDidMount() {
    this.setTimePriceNullJson()
  }

  setTimePriceNullJson = () => {
    {this.state.partitions.map((item,index)=>( 
       this.setState({dayDetails:[...this.state.dayDetails,
        {
          "partitionIndex":index,
          "timings":[
              {"name":"sunday"}
          ],
          "pricings":[
              {"name":"sunday"}
          ]
        }
        
      ]})
    )
    )}
    
  }

  componentDidUpdate(){
    console.log("--> ",this.state.dayDetails)

  }


  render() {
    return (
      <div style={styles.main_container}>
        ...
      </div>
    )
  }
}

export default Step2;

Right now output is coming: this.state.dayDetail output is:现在output来了: this.state.dayDetail output是:

[{"partitionIndex": 2, "pricings": [[Object]], "timings": [[Object]]}]

Desire output is:愿望 output 是:

[
{"partitionIndex": 0, "pricings": [[Object]], "timings": [[Object]]},
{"partitionIndex": 1, "pricings": [[Object]], "timings": [[Object]]},
{"partitionIndex": 2, "pricings": [[Object]], "timings": [[Object]]}
]

Just do everything step by step.一步一步做所有事情。 It will be much shorter and easier to read.它会更短,更容易阅读。

setTimePriceNullJson = () => {
  const dayDetails = this.state.partitions.map((x, i) => {
    return {
      partitionIndex: i,
      timings: [{ name: "sunday" }],
      pricings: [{ name: "sunday" }]
    };
  });
  this.setState({ ...this.state, dayDetails: dayDetails });
};

Clarifications: setState is not reflecting changes to the state object immediatelly, in each iteration of map function state.dayDetails was an empty array, so...this.state.dayDetails was always [], and thats why only last value was set there. Clarifications: setState is not reflecting changes to the state object immediatelly, in each iteration of map function state.dayDetails was an empty array, so...this.state.dayDetails was always [], and thats why only last value was set there .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM