如何将JSON对象映射到JavaScript中的键的数组

发布于 2025-02-04 14:05:37 字数 922 浏览 3 评论 0原文

关于这个主题,有大约有百万个问题(和答案),但是它们都没有做我需要做的事情。我有一个JSON对象,其中每个密钥的值是一个对象。我想将其转换为阵列并维护顶级密钥。

{
  "someKey1": {
    active: true,
    name: "foo"
  },
  "someKey2": {
    active: false,
    name: "bar"
  }
}

如果我使用object.keys(),我会得到顶级键,但没有它们的值。如果我使用object.values(),我会得到一个带有所有值的数组,而不是它们的键。我正在尝试使用钥匙和地图,但是只能返回值:

const data = {
  "someKey1": {
    active: true,
    name: "foo"
  },
  "someKey2": {
    active: false,
    name: "bar"
  }
}
const items = Object.keys(data).map(function(key) {
    return data[key];
});

// returns [{active: true, name: foo},{active: false, name: bar}]

有没有办法获得两者?我想获得一个数组,我可以迭代这样的外观:

[{
    key: someKey1,
    active: true,
    name: "foo"
},
{
    key: someKey2,
    active: true,
    name: "foo"
}]

OR

[
"someKey1": {
    active: true,
    name: "foo"
},
"someKey2": {
    active: false,
    name: "bar"
}
]

There's about million questions (and answers) out there on this topic, but none of them are doing what I need to do. I have a JSON object where the value for each key is an object. I want to convert this to an array and maintain the top level keys.

{
  "someKey1": {
    active: true,
    name: "foo"
  },
  "someKey2": {
    active: false,
    name: "bar"
  }
}

If I use Object.keys() I get the top level keys, but not their values. If I use Object.values() I get an array with all the values, but not their keys. I'm trying to use keys and map, but am only getting the values returned:

const data = {
  "someKey1": {
    active: true,
    name: "foo"
  },
  "someKey2": {
    active: false,
    name: "bar"
  }
}
const items = Object.keys(data).map(function(key) {
    return data[key];
});

// returns [{active: true, name: foo},{active: false, name: bar}]

Is there a way to get both? I want to get an array I can iterate over that looks something like this:

[{
    key: someKey1,
    active: true,
    name: "foo"
},
{
    key: someKey2,
    active: true,
    name: "foo"
}]

OR

[
"someKey1": {
    active: true,
    name: "foo"
},
"someKey2": {
    active: false,
    name: "bar"
}
]

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

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

发布评论

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

评论(2

花落人断肠 2025-02-11 14:05:37

我认为您要朝着正确的方向前进,如果您想添加“键”属性,则必须手动映射属性,以及第二个选项,因为您不需要“键”属性更优雅的是:

对于第一个选项:

Object.keys(data).map(v => ({
    key: v,
    ...data[v]
}));

对于第二种选项,更简单:

Object.keys(data).map(v => ({[v]: {...data[v]}}))

I think you are going in the right direction, if you want to add the "key" property you have to map the properties manually and for the second option since you don't need the "key" property it can be done a little bit more elegantly:

For the first option:

Object.keys(data).map(v => ({
    key: v,
    ...data[v]
}));

For the second option even simpler:

Object.keys(data).map(v => ({[v]: {...data[v]}}))
云巢 2025-02-11 14:05:37

您可以轻松地将数据映射到新对象:

Object.keys(data).map(key => ({ ...data[key], "key": key }));

You can easily map your data to a new object:

Object.keys(data).map(key => ({ ...data[key], "key": key }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文