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

时间:2017-04-08 01:23:13

标签: javascript

我需要递归地在对象上添加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"
        }
      ]
    }
  ]
}

2 个答案:

答案 0 :(得分:2)



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);




答案 1 :(得分:0)

    // 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
            }
        }
    }