gpt4 book ai didi

javascript - 如何将带有连字符的键的对象解构为变量?

转载 作者:行者123 更新时间:2023-12-03 02:04:57 25 4
gpt4 key购买 nike

如何从键包含连字符的对象解构属性?

例如:

{
accept-ranges:"bytes",
cache-control:"public, max-age=0",
content-length:"1174",
content-type:"application/json",
date:"Mon, 03 Oct 2016 06:45:03 GMT",
etag:"W/"496-157892e555b"",
last-modified:"Mon, 03 Oct 2016 06:14:57 GMT",
x-powered-by:"Express"
}

现在获取content-typex-powered-by使用解构从对象中获取值?

最佳答案

就像不能用连字符声明变量一样,也不能直接解构为变量。您需要将变量重命名为其他名称才能在当前范围内访问它。您可以使用以下解构语法来执行此操作:

const x = {
"accept-ranges":"bytes",
"cache-control":"public, max-age=0",
"content-length":"1174",
"content-type":"application/json",
date:"Mon, 03 Oct 2016 06:45:03 GMT",
etag:"W/496-157892e555b",
"last-modified":"Mon, 03 Oct 2016 06:14:57 GMT",
"x-powered-by":"Express"
};
const { "accept-ranges": acceptRanges } = x;
console.log(acceptRanges); // "bytes"

关于javascript - 如何将带有连字符的键的对象解构为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39825868/

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