Convery JS对数组的对象 - 保持钥匙&价值

发布于 2025-02-11 18:17:51 字数 450 浏览 1 评论 0原文

我正在尝试将对象(更新)转换为数组(配置),同时保持相同的结构(并避免进一步嵌套)。

我尝试声明一个新的数组const并使用对象。进入键&值。 我能够获得钥匙,但是很难弄清楚如何实现阵列的嵌套。

const configArray = [];
    
    Object.entries(updatedConfig).forEach(([key, value]) => {
        configArray.push(key);     
    })

这是有问题的对象:

I am trying to convert an object (updatedConfig) to an array (configArray), while maintaining the same structure (and avoid further nesting).

I have tried declaring a new array const and using Object.entries to push the keys & values.
I am able to get the keys but am having trouble figuring out how to achieve the nesting of array.

const configArray = [];
    
    Object.entries(updatedConfig).forEach(([key, value]) => {
        configArray.push(key);     
    })

Here is the object in question:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

今天小雨转甜 2025-02-18 18:17:51

您可以使用object.entriesarray.map尝试这样的尝试。

const configObject = {
 key1: 'value',
 key2: 1,
 key3: [1, 2, 3]

}

const configArray = Object.entries(configObject).map(([key, value]) => ({key, value}))

console.log(configArray)

You can try something like this using Object.entries and Array.map

const configObject = {
 key1: 'value',
 key2: 1,
 key3: [1, 2, 3]

}

const configArray = Object.entries(configObject).map(([key, value]) => ({key, value}))

console.log(configArray)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文