简体   繁体   English

过滤 JavaScript 中最多“n”层的嵌套对象数组

[英]Filtering array of nested objects in JavaScript up to 'n' levels

I want to filter the array of nested objects in javascript.我想过滤javascript中的嵌套对象数组。

I got the below answer from other question .我从其他问题得到以下答案。

var sampleData= [{       
        "rowId": "3.0.0",
        "startDate": "2020-10-20",
        "subTasks": [                 
            {                
                "rowId": "3.3.0",
                "startDate": "2021-05-26",             
                "subTasks": [
                    {
                        "rowId": "3.3.0.1",
                        "startDate": "2021-05-26",                        
                        "subTasks": []
                    },
                    {
                        "rowId": "3.3.0.2",
                        "startDate": "2021-06-09",
                        "endDate": "2021-07-23",  
                        "subTasks": []                      
                    },                   
                ]
            },           
        ]
    }]

    filtered = sampleData.map(element => {
        return {
          ...element,
          subTasks: element.subTasks.filter(subElement => {
            return subElement.endDate
          })
        }
      })
      console.log("sampleData",JSON.stringify(filtered))




I want to filer based on the end date.我想根据结束日期进行归档。 Expected output: Object with rowId "3.3.0.2" need to be filtered.预期 output: Object rowId "3.3.0.2" 需要过滤。

This code is filtering only up to 2 levels.此代码最多只能过滤 2 个级别。 But in my case the nested objects can grow up to 10 levels.但在我的例子中,嵌套对象最多可以增长到 10 层。 How can I filter the array of objects up to n levels?如何过滤最多 n 级的对象数组?

You can do it with a recursive function and loop over only the valid subtasks.您可以使用递归 function 并仅循环有效的子任务。 I do not think it needs much of an explanation, assuming you know what a recursive function is (and I guess you do):我不认为它需要太多解释,假设你知道递归 function 是什么(我猜你知道):

function recurseOnSubTasks(elem) {
  const filteredSubTasks = elem.subTasks
    .filter(subTask => !subTask.endDate)
    .map(subTask => recurseOnSubTasks(subTask))
  
  return {
    ...elem,
    subTasks: filteredSubTasks
  }
}

And then call it with:然后调用它:

sampleData.map(elem => recurseOnSubTasks(elem))

 var sampleData= [{ "rowId": "3.0.0", "startDate": "2020-10-20", "subTasks": [ { "rowId": "3.3.0", "startDate": "2021-05-26", "subTasks": [ { "rowId": "3.3.0.1", "startDate": "2021-05-26", "subTasks": [] }, { "rowId": "3.3.0.2", "startDate": "2021-06-09", "endDate": "2021-07-23", "subTasks": [] }, ] }, ] }]; function recurseOnSubTasks(elem) { const filteredSubTasks = elem.subTasks.filter(subTask =>.subTask.endDate).map(subTask => recurseOnSubTasks(subTask)) return {..,elem: subTasks. filteredSubTasks } } console.log(sampleData;map(elem => recurseOnSubTasks(elem)));

you can do that by using recursion.你可以通过使用递归来做到这一点。 values array will contain all tasks then you can do filtration on any property you want. values 数组将包含所有任务,然后您可以对任何您想要的属性进行过滤。

var values = [];
function GetValues(samples){

    if(samples != [] && samples != undefined && samples != null){
        for(var sample of samples){       
            values.push(sample)
            GetValues(sample.subTasks)
                                  }

    }else{
        return {};
    }

}
GetValues(sampleData)

If you want to filter based on endDate, then can use forEach method and put it in an object and then reach out to desired output. Hope this would work.如果您想根据 endDate 进行过滤,则可以使用 forEach 方法并将其放入 object,然后到达所需的 output。希望这会起作用。

   var sampleData= [{       
        "rowId": "3.0.0",
        "startDate": "2020-10-20",
        "subTasks": [                 
            {                
                "rowId": "3.3.0",
                "startDate": "2021-05-26",             
                "subTasks": [
                    {
                        "rowId": "3.3.0.1",
                        "startDate": "2021-05-26",                        
                        "subTasks": []
                    },
                    {
                        "rowId": "3.3.0.2",
                        "startDate": "2021-06-09",
                        "endDate": "2021-07-23",  
                        "subTasks": []                      
                    },                   
                ]
            },           
        ]
}]

  sampleData.forEach((obj)=> {
       obj.subTasks.forEach((prop) => {
           const newObj = {name: prop};
           console.log(newObj.name.subTasks[1]);
           console.log(newObj.name.subTasks[1].endDate);
       })
    })

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

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