【发布时间】:2020-09-02 21:04:56
【问题描述】:
我真的被困在这里了。请帮忙!
我有一个这样的数组:
0:{
area: {acres: "249,561.00", square_km: "1,009.9"}
coordinates: {latitude: 34.01, longitude: -119.42}
id: "park_channel-islands"
title: "Channel Islands"
visitors: "364,807}
1: {area: {…}, coordinates: {…}, date_established_readable...
2: .....
3: ....
这是我在构造函数this.state ={coodLabell:[]}中的状态
我想通过在数组中创建字段坐标和字段标题的对象来填充数组协调器。
例如:在索引 0 中。我想创建对象 {"title": title, "cood": coordinates" 并将其推送到构造函数中的空数组协调器中。
这是我尝试过的,但我总是取回空数组
array.forEach((element) => {
const title = element.title
const cood = element.coordinates
const newData = {"title": title, "cood": cood}
this.setState({coodLabell:[...this.state.coodLabell,...newData]})
如果你们能给我一些提示,我很感激。谢谢你
【问题讨论】:
标签: arrays reactjs object setstate