在不为每个对象分配变量的情况下解构对象数组
Destructing array of objects without assigning each a variable
我查看了堆栈,但找不到我的问题的答案,如果它是重复的,我真的很抱歉。
我有一个对象数组:
export const people = [
{
id: 1,
name: "Thom"
},
{
id: 2,
name: "Bob"
}
];
我正在导入这个文件:
import {people} from '../Person';
我正在分配状态:
state = {
peopleData: people
}
我正试图像这样破坏这个数组,但不幸的是,我不允许这样做:
const{people} = this.state.peopleData;
我无法对我的人员使用 map
函数进行迭代的原因是什么?
{people.map(person=> (<Person key={person.id} personInfo={person}/>))}
我当然可以这样做,但我不想这样做,因为如果我需要重复几次,我将需要始终编写相同的代码 this.state.peopleData
...:
{
this.state.peopleData.map(person=> (<Person key={person.id} personInfo={person}/>
))}
I'm assigning the state:
state = {
peopleData: people
}
And I'm trying to destruct this array like this, but unfortunately, I'm not allowed to do that:
const{people} = this.state.peopleData;
如果没有解构,可以是这样的:
const people = this.state.peopleData;
或者这个解构:
const {peopleData: people} = this.state;
...表示“从 this.state
中获取 属性 peopleData
的值并将其放入常量 people
.
我查看了堆栈,但找不到我的问题的答案,如果它是重复的,我真的很抱歉。
我有一个对象数组:
export const people = [
{
id: 1,
name: "Thom"
},
{
id: 2,
name: "Bob"
}
];
我正在导入这个文件:
import {people} from '../Person';
我正在分配状态:
state = {
peopleData: people
}
我正试图像这样破坏这个数组,但不幸的是,我不允许这样做:
const{people} = this.state.peopleData;
我无法对我的人员使用 map
函数进行迭代的原因是什么?
{people.map(person=> (<Person key={person.id} personInfo={person}/>))}
我当然可以这样做,但我不想这样做,因为如果我需要重复几次,我将需要始终编写相同的代码 this.state.peopleData
...:
{
this.state.peopleData.map(person=> (<Person key={person.id} personInfo={person}/>
))}
I'm assigning the state:
state = { peopleData: people }
And I'm trying to destruct this array like this, but unfortunately, I'm not allowed to do that:
const{people} = this.state.peopleData;
如果没有解构,可以是这样的:
const people = this.state.peopleData;
或者这个解构:
const {peopleData: people} = this.state;
...表示“从 this.state
中获取 属性 peopleData
的值并将其放入常量 people
.