繁体   English   中英

在嵌套对象上递归添加字段

[英]Recursively add fields on a nested object

我需要递归在对象上添加uuid,如何使用javascript做到这一点?

我的想法是BFS / DFS都能正常工作。

我的点子

function addIdRecursively(root){
  if(root is a Object){
    ITERATE_EACH_ATTR_IN
    root.id = 'UUID'
  }
  else if(root is a array){
    root.map(item=>addIdRecursively(item))
  }
}

输入

{
  "viewType": "List View",
  "sections": [
    {
      fields:[
      'Id',
      'Name'
      ]
    },  
    {
      "children": [
        {
          "name": "no"
        }
      ]
    }
  ]
}

预期结果

{
  "id": "ef910f30-fe25-0134-8f50-745c898f0819",
  "viewType": "List View",

  "sections": [
    {
      "id": "ef910f30-fe25-0134-8f50-745c898f0819",
      fields:[
      'Id',
      'Name'
      ]
    },  
    {
      "id": "ef910f30-fe25-0134-8f50-745c898f0819",
      "children": [
        {
          "id": "ef910f30-fe25-0134-8f50-745c898f0819",
          "name": "no"
        }
      ]
    }
  ]
}

 let uuid = "ef910f30-fe25-0134-8f50-745c898f0819"; function addRecursively(obj) { if(Array.isArray(obj)) // if obj is an array obj.forEach(addRecursively); // then call addRecursively on all its items else if(obj && typeof obj === "object") { // otherwise if it's an object (the "obj &&" part is needed because typeof null === "object") obj.id = uuid; // add the id property to it Object.keys(obj).forEach(k => addRecursively(obj[k])); // and then call add recursively on all its items } } let obj = {"viewType":"List View","sections":[{"fields":["Id","Name"]},{"children":[{"name":"no"}]}]}; addRecursively(obj); console.log(obj); 

    // Generate a v4 UUID (random)
    import stringMatching = jasmine.stringMatching;
    import {log} from "../Util";
    const uuidV4 = require('uuid');
    // uuidV4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

    export default class UUIDGenerator {
        structure = null
        constructor(structure) {
            this.structure =     {
                "viewType": "List View",
                "sections": [
                    {
                        fields:[
                            'Id',
                            'Name'
                        ]
                    },
                    {
                        "children": [
                            {
                                "name": "no"
                            }
                        ]
                    }
                ]
            }
        }
        process(){
            this.addIdRecursively(this.structure)
            log(this.structure)
            debugger
            return this.structure
        }

        addIdRecursively(structure){
            switch (typeof  structure){
                case 'array':
                    return structure.map(item => this.addIdRecursively(item))
                case 'object':
                    for (var key in structure) {
                        this.addIdRecursively(structure[key])
                    }
                    structure.id = uuidV4()
                    return structure
            }
        }
    }

暂无
暂无

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

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