gpt4 book ai didi

javascript - 在最新的 JavaScript 中,这个更短的符号是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:07 25 4
gpt4 key购买 nike

我有这个:

const id = speakerRec.id;
const firstName = speakerRec.firstName;
const lastName = speakerRec.lastName;

而且我认为有这样的东西但不记得了。

const [id, firstName, lastName] = speakerRec;

最佳答案

您需要使用 {} 来解构对象属性:

const {id, firstName, lastName} = speakerRec;

[] 用于数组解构:

const [one, two, three] = [1, 2, 3];

演示:

const speakerRec = {
id: "mySpeaker",
firstName: "Jack",
lastName: "Bashford"
};

const { id, firstName, lastName } = speakerRec;

console.log(id);
console.log(firstName);
console.log(lastName);

const [one, two, three] = [1, 2, 3];

console.log(one);
console.log(two);
console.log(three);

关于javascript - 在最新的 JavaScript 中,这个更短的符号是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com