gpt4 book ai didi

javascript - 如何将 JS 键控对象转换为键控数组,同时跳过未定义?

转载 作者:行者123 更新时间:2023-11-28 14:17:02 25 4
gpt4 key购买 nike

如何转换这个JS对象:

users = {
1: { id: 1, name: "..." },
2: { id: 2, name: "..." },
// 3 is missing
4: { id: 4, name: "..." },
...
}

进入数组:

usersArray = [
1: { id: 1, name: "..." },
2: { id: 2, name: "..." },
// 3 is missing -- I do not want undefined here
4: { id: 4, name: "..." },
...
]

关键是我不要未定义的空格,关键必须是对象Id。我不想要空白/未定义条目的原因是因为我使用 vue.js 并且我非常确定它不会迭代 v-for 中具有未定义值的数组.

类似:https://forum.vuejs.org/t/vuex-best-practices-for-complex-objects/10143/2但没有额外的数组只存储 id。

这可能吗?

最佳答案

您可以使用Object.values()

const users = {
1: { id: 1, name: "..." },
2: { id: 2, name: "..." },
4: { id: 4, name: "..." },
};
console.log(Object.values(users))

关于javascript - 如何将 JS 键控对象转换为键控数组,同时跳过未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57008516/

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