JSON ForEach in Node.JS



我已经使用Node.JS编写了一个url shortener,并保存在json数据中。现在我正在为我的团队编写一个Web Dashboard来管理url。我喜欢使用forEach,因为我可以很容易地使用它为每个条目向我的html站点添加代码。我的问题:我有这样的json数据:


{
"support": {
"url": "https://this-is-the-url.end",
"author": "Name of the Author"
},
"invite": {
"url": "https://an-other-url.end",
"author": "Name of the Author"
},
.
.
.
}

我不知道如何分割它,这样我就可以使用

Object.forEach(json => {
var author = json.author
var url = json.url
*add code to html code*
})

我已经在Stackoverflow上搜索过了,但找不到什么。有人能帮我吗?

这样?

Object.keys(bigJson).forEach(key => {
const json = bigJson[key];
const author = json.author;
});

(注意,bigJsonjson不是实际的JSON字符串,它们是对象,但我想保留OP的命名,以避免在这一点上混淆。(

可能是这样的:

import json from '/some/path/to/urls.json';
Object.entries(([key, value]) => {
doSthWith(key); // 'support', 'invite';
doSthElseWith(value.url, value.author);
})

最新更新