【问题标题】:recursively create a nested object from other object in javascript从javascript中的其他对象递归地创建一个嵌套对象
【发布时间】:2020-06-27 22:58:59
【问题描述】:

我是编程新手,我正在研究一个困扰我一段时间的问题。

我想在javascript中从另一个嵌套对象递归地创建一个嵌套对象,

以下是输入的示例数据,但在实际情况下,我不知道这个对象有多深。

nums = {
    Obj:{
        x1:{
            x11:43,
            x12:4,
            x13:612
        },
        x2:{
            x21:4,
            x22:7,
        },
        x3:2,
    }}

这是我想要的结果(看数字是偶数还是奇数,偶数=真,奇数=假)

res = {
    Obj:{
        x1:{
            x11:false,
            x12:true,
            x13:true
        },
        x2:{
            x21:true,
            x22:false,
        },
        x3:true,
    }}

这是我的代码

const nums = {
    Obj:{
        x1:{
            x11:43,
            x12:4,
            x13:612
        },
        x2:{
            x21:4,
            x22:7,
        },
        x3:2,
    }
}
const res ={};

getResult(nums);

console.log(res);

function getResult(x){
    Object.keys(x).forEach(element => {
        if(isNaN(x[element])){
            res[element]=getResult(x[element]);
        } else {
            let result = (x[element] % 2 < 1)? true:false;
            return {[element]: result}; // this is where I don't know what to, I try to return a object, 
                                        //  but it gives{x1: undefined, x2: undefined, Obj: undefined}
                                        //
                                        // if I change to "return res[element]=result"
                                        // every sub-Object will add under the same level
        }
    });
}

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: javascript recursion nested-object


    【解决方案1】:

    而不是return {[element]: result};,覆盖值,并在循环后从函数返回变异对象:

    请注意,这会改变原始对象,如果您想保留它,请复制:

    const copy = JSON.parse(JSON.stringify(nums));

    const nums = {
      Obj: {
        x1: {
          x11: 43,
          x12: 4,
          x13: 612
        },
        x2: {
          x21: 4,
          x22: 7,
        },
        x3: 2,
      }
    }
    const res = {};
    const copy = JSON.parse(JSON.stringify(nums));
    
    getResult(copy);
    
    console.log(res);
    
    function getResult(x) {
      Object.keys(x).forEach(element => {
        if (isNaN(x[element])) {
          res[element] = getResult(x[element]);
        } else {
          let result = (x[element] % 2 < 1) ? true : false;
          x[element] = result; // overwrite the number with true or flse
        }
      });
      return x; // return the mutated object
    }

    【讨论】:

      【解决方案2】:

      与其改变某些东西,不如让某些东西更具功能性并返回一个新对象:

      const getResults = o => typeof o === "object"
          ? Object.keys(o).reduce((a, k) => ({ ...a, [k]: getResults(o[k]) }), {})
          : o % 2 === 1;
      

      基本上,我们检查对象是否是对象(使用 typeof),如果是,则更深入。否则我们检查它是奇数还是偶数。

      【讨论】:

        【解决方案3】:

        您也可以更一般地考虑这一点,编写一个函数,将您的转换应用于对象的所有叶节点,然后使用 isEven 函数调用它。这是一种技术:

        const mapLeaves = (fn) => (tree) =>
          typeof tree == "object"
            ? Object .fromEntries (Object .entries (tree) .map (
                ([k, v]) => [k, mapLeaves (fn) (v)]
              ))
            : fn (tree)
         
        const isEven = (n) => n % 2 == 0
        
        const nums = {Obj: {x1: {x11: 43, x12: 4, x13: 612}, x2: {x21: 4, x22: 7}, x3: 2}}
        
        console .log (
          mapLeaves (isEven) (nums)
        )

        当然,mapLeaves (isEven) 是一个可重用的函数,您可以将其应用于多个对象。

        这不处理数组。创建 mapLeaves 的版本并将其应用于数组条目只会稍微复杂一些:

        const mapLeaves = (fn) => (tree) =>
          Array .isArray (tree)
            ? tree .map (x => mapLeaves (fn) (x))
          : typeof tree == "object"
            ? Object .fromEntries (Object .entries (tree) .map (
                ([k, v]) => [k, mapLeaves (fn) (v)]
              ))
          : fn (tree)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-04-20
          • 2021-06-07
          • 1970-01-01
          • 2020-12-21
          • 2020-10-06
          • 2012-02-12
          相关资源
          最近更新 更多