gpt4 book ai didi

javascript - 如何将对象更改为数组

转载 作者:行者123 更新时间:2023-11-30 19:28:57 26 4
gpt4 key购买 nike

我有一个对象需要更改为数组。什么是最好的方法?

var x = {
vehicle: {
manufacturer: "Volkswagen",
carlineName: "Golf",
}
}

我想要下面的结果

var x = {
vehicle: [
{
manufacturer: "Volkswagen",
carlineName: "Golf",
}
]
}

最佳答案

对于这个例子,你可以这样写:

var x = {
vehicle: {
manufacturer: "Volkswagen",
carlineName: "Golf",
}
}

x.vehicle = [x.vehicle]

console.log(x)

x.vehicle 获取

{
manufacturer: "Volkswagen",
carlineName: "Golf",
}

和方括号[x.vehicle]将该值放入一个数组中:

[{
manufacturer: "Volkswagen",
carlineName: "Golf",
}]

我们想用这个新值重新分配 x.vehicle,所以我们使用 x.vehicle = [x.vehicle]

关于javascript - 如何将对象更改为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56634212/

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