简体   繁体   English

将json转换为嵌套的JavaScript对象

[英]Convert json into nested JavaScript Object

I have a json int file that contains any items like below: 我有一个json int文件,其中包含以下任何项目:

Input JSON 输入JSON

{
    "action.button.submit": "Submit"
    "action.button.submitting": "Submitting"
    "buttons.common.add": "Add"
    "buttons.common.reset": "Reset"
    "constants.bom.conditional.or.mandatory.conditional": "Conditional"
    "constants.bom.conditional.or.mandatory.mandatory": "Mandatory"
}

Output 产量

{
   action: {
       button: {
           submit: 'Submit'
           submitting: 'Submitting'
       }
   },
   buttons: {
       common: {
           add: 'Add',
           reset: 'Reset'
       }
   },
   constants: {
      bom: {
         conditional: { 
            or: {
                mandatory:{
                   conditional: 'Conditional',
                   mandatory: 'Mandatory'
               }
            }
         }
      }
   }
}

This was as far as I could get: 据我所知:

newData = {};
Object.keys(data).forEach(item => {
    const splitData = item.split('.');
    splitData.forEach((detail, index) => {
        if(index === 0 && !newData[detail]) newData[detail] = {};
    })
});
console.info(newData)

I would like to take the Input and make it look like the output 我想接受Input并使它看起来像output

You could use one forEach loop on object entries and then inside split each key on . 您可以在对象条目上使用一个forEach循环,然后在上拆分每个键. . After that you can use reduce method on that array of keys to build nested object. 之后,您可以在该键数组上使用reduce方法来构建嵌套对象。

 const obj = { "action.button.submit": "Submit", "action.button.submitting": "Submitting", "buttons.common.add": "Add", "buttons.common.reset": "Reset", "constants.bom.conditional.or.mandatory.conditional": "Conditional", "constants.bom.conditional.or.mandatory.mandatory": "Mandatory" } const res = {} Object.entries(obj).forEach(([key, value]) => { key.split('.').reduce((r, e, i, a) => { return r[e] || (r[e] = (a[i + 1] ? {} : value)) }, res) }) console.log(res) 

Using Lodash you can do this with _.set method that takes target object, nested key and value. 使用Lodash,您可以通过_.set方法执行此操作, _.set方法采用目标对象,嵌套键和值。

 const obj = {"action.button.submit": "Submit","action.button.submitting": "Submitting","buttons.common.add": "Add","buttons.common.reset": "Reset","constants.bom.conditional.or.mandatory.conditional": "Conditional","constants.bom.conditional.or.mandatory.mandatory": "Mandatory"} const res = {} _.forEach(_.entries(obj), ([k, v]) => _.set(res, k, v)) console.log(res) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script> 

You have to recursively traverse deep into the resulting object: 您必须递归地遍历结果对象:

function parse(obj) {
  const root = {};

  for(const [key, value] of Object.entries(obj)) {
    const parts = key.split(".");
    const parent = parts.slice(0, -1).reduce((acc, part) => acc[part] || (acc[part] = {}), root);
    parent[parts.pop()] = value;
  }

  return root;
}

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

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