1

我很好奇是否有可能在它被解构的同一行上返回一个解构对象。

当前(工作)示例:

使用 2 行

const itemList = json.map((item) => {
    const { id, title } = item;
    return { id, title };
});

1 行但未解构

const itemList = json.map((item) => {
    return { id: item.id, title: item.title }; // This also requires repeating the field name twice per instance which feels hacky
});

有没有可能将身体凝聚成一条线?

示例(不起作用)

const itemList = json.map((item) => {
    return { id, title } = item;
}
4

1 回答 1

2

解构回调参数,并返回一个对象:

const itemList = json.map(({ id, title }) => ({ id, title }))
于 2020-12-18T16:48:28.763 回答