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

Javascript 使用带.reduce()的递归从嵌套对象数组中删除特定属性,javascript,arrays,object,recursion,data-manipulation,Javascript,Arrays,Object,Recursion,Data Manipulation,我正在使用这个嵌套对象数组,它是从API获取的: const myObj = [ { "$id":"1", "Description":"WA State", "Place":"WA", "Data":[ { "$id":"2", "Description":"Years", "Indicators":[ {

我正在使用这个嵌套对象数组,它是从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属性,但不要修改对象。我正在尝试使用。减少:

而且不起作用。有没有关于我如何使用。减少


PD:我的问题不是重复的,因为我提到并指定使用reduce来实现目标。在另一个问题中,答案是关于使用for…循环语法。

您忘记检查属性是对象还是对象数组。此外,您还使用映射函数的prop参数来浅显removeKeys的prop参数。基本上,此代码将实现以下功能:

const props=['$id','Description']; 函数removeKeysobj{ 返回Object.keysobj.reduceobject,key=>{ if Array.isArrayobj[键]{ object[key]=obj[key].mapitem=>RemoveKeyItem; } 否则,如果对象的类型[键]=“对象”{ console.logobject; 对象[key]=removeKeysobj[key]; } 否则,如果props.indexOfkey==-1{ 对象[键]=对象[键]; } 返回对象; }, {}; } console.log removeKeysmyObj; myObj= { $id:1, 描述:佤邦, 地点:西澳, 数据:[ { $id:2, 描述:年, 指标:[ { $id:3, 年份:2017年, 积分:22191, 目标:28000, 说明:2017年 }, { $id:4, 年份:2018年, 积分:25994, 目标:28000, 描述:2018年 } ] }, { $id:5, 描述:本地目标, 指标:[ { $id:6, 年份:2018年, 积分:25994, 目标:28000, 描述:2018年 } ] }, { $id:7, 描述:远程目标, 指标:[ { $id:8, 年份:2018年, 积分:55857, 目标:84000, 描述:2018年 } ] } ] };
为什么需要使用reduce来实现呢?因为我不想改变状态,而且我正在寻找一种优雅的方式来实现它。首先,不要担心优雅,而是担心一些有效的东西。听起来你想要一个单独的新对象数组?是的,但是。。。使用reduce很难做到这一点?为什么有人否决了我的答案?我已经用问题作者选择的方法提出了解决方案。有人否决我的帖子的原因是什么?我已经用问题作者所选择的方法提出了解决方案,请沃洛德梅尔回答。我对你的答案投了赞成票。但是,我也读过,在reduce中使用spread操作符是可能的,正如您在主题Deleting properties中看到的那样。你也知道吗?是的,你可以使用扩展操作符Object.assign也可以做这个技巧。但是,您需要迭代属性并检查它们是否为对象。为什么?因为spread运算符不进行深度克隆。因此,如果您将使用扩展运算符克隆原始对象,然后更改嵌套在其中的对象中的某些内容,您也将对原始对象进行变异。如果解决了问题,您也可以将此答案标记为正确答案。请使用object.assign或spread运算符发布答案?这是为了理解。
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