我有一个文件 version.json:
{
"components":{
"toto": {
"test": [
{
"name": "test-toto",
"type": "html"
}
]
},
"tata": {
"test": [
{
"name": "test-tata",
"type": "html"
}
]
},
"titi": {
"test": [
{
"name": "test-titi",
"type": "html"
}
]
}
"versions": {
"test-toto": "2.1",
"test-tata": "2.2",
"test-titi": "2.3"
}
我有一个文件 version2.json:
{
"components":{
"toto": {
"test": [
{
"name": "test-toto",
"type": "html"
}
]
},
"tata": {
"test": [
{
"name": "test-tata",
"type": "html"
}
]
},
"titi": {
"test": [
{
"name": "test-tata",
"type": "html"
}
]
}
"versions": {
"test-toto": "1.1"
"test-tata": "1.2",
"test-titi": "2.3"
}
我正在尝试使用以下语法创建 JSON:
{
components: "",
newVersion: "",
oldVersion: "",
}
我只想推送版本与其组件名称不同的版本,如下所示:
[{
components: "toto",
newVersion: "2.1",
oldVersion: "1.1"
},
{
components: "tata",
newVersion: "2.2",
oldVersion: "1.2",
}
]
我在做什么:
public async listDiffer(): Promise<any[]> {
const showVersion = fs.readFileSync("versions.json", "utf8");
const json1 = JSON.parse(showVersion);
const list1 = json1.versions;
const showVersion2 = fs.readFileSync("versions2.json", "utf8");
const json2 = JSON.parse(showVersion2);
const list2 = json2.versions;
const diffList: Array<{ components: string; newVersion: string; oldVersion: string }> = [];
const keysFromHead = Object.keys(list1);
keysFromHead.forEach((key) => {
if (list1[key] !== list2[key]) {
diffList.push({
components: ,
newVersion: list1[key],
oldVersion: list2[key],
});
}
});
});
return diffList;
}
我有什么:
[{
components: ,
newVersion: "2.1",
oldVersion: "1.1"
},
{
components: ,
newVersion: "2.2",
oldVersion: "1.2",
}
]
如何检索组件名称(toto 和 tata) 感谢您的帮助
最佳答案
1) 您的 JSON 有语法错误 - 您可以检查错误并修复它:) ==> 这里 https://jsonformatter.curiousconcept.com/
关于javascript - 在迭代中检索对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60339728/