简体   繁体   English

Json 数据修改成新的键值对 JavaScript

[英]Json data modify into new key value pair JavaScript

I have a json data that I want to reformat into new key-value pairs.我有一个 json 数据,我想将其重新格式化为新的键值对。 I am using following javascript code to format my json data.我正在使用以下 javascript 代码来格式化我的 json 数据。

const input = {
    "file1": {
        "function1": {
            "calls": {
                "405:4": {
                "file": "file1",
                "function": "function2"
                },
                "406:4": {
                    "file": "file1",
                    "function": "function3"
                }
            }
        },
        "function2": {
            "calls": {
                "397:7": {
                    "file": "file2",
                    "function": "function1"
                }
            }
        },
        "function3": {
            "calls": null
        },
        "function4": {
            "calls": null
        }
    },
    "file2": {
        "function5": {
            "calls": {
                "43:4": {
                    "file": "file2",
                    "function": "function5"
                }
            }
        },
        "function6": {
            "calls": {
                "32:4": {
                    "file": "file1",
                    "function": "function1"
                }
            }
        }
    }
}
function transformData(data) {
    let  res = [];
    let calls = [];
    Object.entries(data).map(([fileName, fileObject]) => {
        Object.entries(fileObject).map(([functionName, functionObject]) => {
            Object.entries(functionObject).map(([functionKey, functionValue]) => {
                if(functionKey === "calls") {
                    Object.entries(functionValue).map(([callKey, callObject]) => {
                        calls = [...calls, callObject['file']+"."+callObject['function']]
                    });
                } 
            });
            res = [...res,{"name": fileName+"."+functionName, "import": calls}]  
        });
    });
    return res;
}
const sanitize = (obj) => {
    return JSON.parse(JSON.stringify(obj, (key, value) => {
        return (value === null ? undefined : value);
    }));
};
const data = sanitize(input)
const result = transformData(data);
console.log(result)

My expected json data is following:我预期的 json 数据如下:

[
  {
    "name": "file1.function1",
    "imports": [
      "file1.function2",
      "file1.function3"
    ]
  },
  {
    "name": "file1.function2",
    "imports": [
      "file2.function1"
    ]
  },
  {
    "name": "file1.function3",
    "imports": []
  },
  {
    "name": "file1.function4",
    "imports": []
  },
  {
    "name": "file2.function5",
    "imports": [
      "file2.function5"
    ]
  },
  {
    "name": "file2.function6",
    "imports": [
      "file1.function1"
    ]
  }
]

My output is not correct.我的输出不正确。 Though its giving right amount of main array with name and imports keys but the import array is wrong.虽然它提供了适量的带有名称和导入键的主数组,但导入数组是错误的。

Can someone please help me.有人可以帮帮我吗。 I think i'm not returning it in the correct way.我想我没有以正确的方式退货。

The mistake that you're doing is, you're assigning the same calls array to import each time and not clearing the past elements on subsequent iterations.您正在做的错误是,您每次都将相同的calls数组分配给import而不是在后续迭代中清除过去的元素。 Replace with this code for obtaining the expected outcome.用此代码替换以获得预期结果。

function transformData(data) {
    let res = [];
    Object.entries(data).map(([fileName, fileObject]) => {
        Object.entries(fileObject).map(([functionName, functionObject]) => {
            let calls = []; // Notice this line. I'm creating new array for every iteration. Another way is to clear the array before proceeding.
            Object.entries(functionObject).map(([functionKey, functionValue]) => {
                if(functionKey === "calls") {
                    Object.entries(functionValue).map(([callKey, callObject]) => {
                        calls = [...calls, callObject['file']+"."+callObject['function']]
                    });
                } 
            });
            res = [...res,{"name": fileName+"."+functionName, "import": calls}]  
        });
    });
    return res;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM