gpt4 book ai didi

arrays - 如何在 React Native 中将 JSON 对象转换为数组

转载 作者:行者123 更新时间:2023-12-05 08:26:09 25 4
gpt4 key购买 nike

我正在从 Firebase 获取一个 JSON 对象。我正在尝试将其转换为对象数组。

我可以通过逐个从 Firebase 中获取 child 并将它们添加到数组中来解决这个问题,但是,这对我的情况来说不是一个好的做法。

JSON对象格式如下:

key: {
id1: {some stuff here},
id2: {some other stuff},
...
}

现在我想从这个 JSON 对象中得到的是:

arrayName: [
{id1:{some stuff here},
id2:{some other stuff}
]

希望我的描述能够完全理解。任何帮助将不胜感激

最佳答案

这只是普通的 javascript,与 React Native 无关。顺便说一句,您可以使用 Object.keys 获取包含所有键的数组,然后将字符串映射为对象:

const x = {foo: 11, bar: 42};
const result = Object.keys(x).map(key => ({[key]: x[key]}));
console.log(result);

关于arrays - 如何在 React Native 中将 JSON 对象转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57886116/

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