代码之家  ›  专栏  ›  技术社区  ›  robe007 Leo Aguirre

使用带有.reduce()的递归从嵌套的对象数组中删除特定属性

  •  -1
  • robe007 Leo Aguirre  · 技术社区  · 7 年前

    我正在使用这个嵌套对象数组,从API获取:

    const myObj = [
        {
            "$id":"1",
            "Description":"WA State",
            "Place":"WA",
            "Data":[
            {
                "$id":"2",
                "Description":"Years",
                "Indicators":[
                {
                    "$id":"3",
                    "Year":2017,
                    "Points":22191,
                    "Goal":"28000",
                    "Description":"Year 2017"
                },
                {
                    "$id":"4",
                    "Year":2018,
                    "Points":25994,
                    "Goal":"28000",
                    "Description":"Year 2018"
                }
                ]
            },
            {
                "$id":"5",
                "Description":"Local Goal",
                "Indicators":[
                {
                    "$id":"6",
                    "Year":2018,
                    "Points":25994,
                    "Goal":"28000",
                    "Description":"Year 2018"
                }
                ]
            },
            {
                "$id":"7",
                "Description":"Remote Goal",
                "Indicators":[
                {
                    "$id":"8",
                    "Year":2018,
                    "Points":55857,
                    "Goal":"84000",
                    "Description":"Year 2018"
                }
                ]
            }
            ]
        },
    
        {
            "$id":"9",
            "Description":"NY State",
            "Place":"NY",
            "Data":[
            {
                "$id":"10",
                "Description":"Years",
                "Indicators":[
                {
                    "$id":"11",
                    "Year":2017,
                    "Points":23451,
                    "Goal":"27000",
                    "Description":"Year 2017"
                },
                {
                    "$id":"12",
                    "Year":2018,
                    "Points":21953,
                    "Goal":"26000",
                    "Description":"Year 2018"
                }
                ]
            },
            {
                "$id":"13",
                "Description":"Local Goal",
                "Indicators":[
                {
                    "$id":"14",
                    "Year":2018,
                    "Points":24195,
                    "Goal":"25000",
                    "Description":"Year 2018"
                }
                ]
            },
            {
                "$id":"15",
                "Description":"Remote Goal",
                "Indicators":[
                {
                    "$id":"16",
                    "Year":2018,
                    "Points":80857,
                    "Goal":"90000",
                    "Description":"Year 2018"
                }
                ]
            }
            ]
        }
    ];
    

    我需要删除所有 $id Description 对象的属性, .reduce()

    const props = ['$id', 'Descripcion'];
    
    function removeKeys(obj, prop){
      return props.map( (prop, index) => Object.keys(obj).reduce((object, key) => {
        if (key !== prop[index]) {
          object[key] = obj[key]
        }
        if(object.hasOwnProperty(key))
          removeKeys(obj, prop[index])
        return object
      }, {})
      )
    }
    
    console.log( removeKeys(myObj, props) );
    
    // RangeError: Maximum call stack size exceeded
    

    而且不起作用。你有什么想法可以让我用

    警察局: 我的问题 reduce 实现目标。在另一个问题中,答案是关于使用 "for...loop" 语法。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Volodymyr    7 年前

    prop 的论点 removeKeys 具有 道具 映射函数的参数。基本上,这段代码可以做到:

    const props = ['$id', 'Description'];
    
    function removeKeys(obj){
      return Object.keys(obj).reduce((object, key) => {
         if (Array.isArray(obj[key])) {
           object[key] = obj[key].map(item => removeKeys(item));
         }
         
         else if (typeof obj[key] === 'object') {
           console.log(object);
           object[key] = removeKeys(obj[key]);
         }
         
         else if (props.indexOf(key) === -1) {
           object[key] = obj[key];
         }
         
         return object;
      }, {});
    }
    
    console.log( removeKeys(myObj) );
    <script>
      myObj = 
        {
            "$id":"1",
            "Description":"WA State",
            "Place":"WA",
            "Data":[
            {
                "$id":"2",
                "Description":"Years",
                "Indicators":[
                {
                    "$id":"3",
                    "Year":2017,
                    "Points":22191,
                    "Goal":"28000",
                    "Description":"Year 2017"
                },
                {
                    "$id":"4",
                    "Year":2018,
                    "Points":25994,
                    "Goal":"28000",
                    "Description":"Year 2018"
                }
                ]
            },
            {
                "$id":"5",
                "Description":"Local Goal",
                "Indicators":[
                {
                    "$id":"6",
                    "Year":2018,
                    "Points":25994,
                    "Goal":"28000",
                    "Description":"Year 2018"
                }
                ]
            },
            {
                "$id":"7",
                "Description":"Remote Goal",
                "Indicators":[
                {
                    "$id":"8",
                    "Year":2018,
                    "Points":55857,
                    "Goal":"84000",
                    "Description":"Year 2018"
                }
                ]
            }
            ]
        };
    </script>