gpt4 book ai didi

javascript - 如何将此数组转换为 JavaScript 对象?

转载 作者:行者123 更新时间:2023-11-28 18:39:43 24 4
gpt4 key购买 nike

以下代码循环访问 JavaScript 对象并仅收集数组属性:

const building = this.building
let panoramaList = []
for (let key in building) {
const panoramas = building[key]
if (Array.isArray(panoramas)) {
panoramaList.push({ [key]: panoramas })
}
}
console.log(panoramaList)

换句话说,它需要这样:

{
name: '',
description: ''.
livingroom: Array[0],
study: Array[1],
bedroom: Array[0]
}

并将其变成这样:

[
{ livingroom: Array[0] },
{ study: Array[1] },
{ bedroom: Array[0] }
]

但是,我需要生成的是:

{
livingroom: Array[0],
study: Array[1],
bedroom: Array[0]
}

如何实现这一目标?

最佳答案

更改此:

const building = this.building
let panoramaList = []
for (let key in building) {
const panoramas = building[key]
if (Array.isArray(panoramas)) {
panoramaList.push({ [key]: panoramas })
}
}
console.log(panoramaList)

对此:

const building = this.building
let panoramaList = {}
for (let key in building) {
const panoramas = building[key]
if (Array.isArray(panoramas)) {
panoramaList[key]=panoramas
}
}
console.log(panoramaList)

关于javascript - 如何将此数组转换为 JavaScript 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328745/

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