简体   繁体   中英

Not getting children of object with filter

I'm using a filter on an array with objects that have children. But when i filter on the parent i only get the data i filtered on and not the children of that parent. What am I doing wrong ?

app.get('/api/categories/:category', (req, res) => {
    res.send(getFilteredCategories(req.params.category));
  });


function getFilteredCategories(category){
    const categorieArray = [categories];
    const filteredCategory = categorieArray.filter(d => d.productCategoryLevel1Description = category);
    console.log(filteredCategory);
    return filteredCategory;   
}

for example when i go " http://localhost:3000/api/categories/Cartons " I'm getting

[
{
"productCategoryLevel1Description": "Cartons"
}
]

but what i want is :

"productCategoryLevel1": "1",
"productCategoryLevel1Description": "Cartons",
"sortingKeyLevel1": 1,
"children": [
{
"productCategoryLevel2": "1.1",
"productCategoryLevel2Description": "Product Category 1.1",
"productCategoryLevel1": "1",
"children": [
{
"productCategoryLevel3": "1.1.1",
"productCategoryLevel3Description": "Product Category 1.1.1",
"productCategoryLevel2": "1.1",
"categoryCount": 3
},
{
"productCategoryLevel3": "1.1.2",
"productCategoryLevel3Description": "Product Category 1.1.2",
"productCategoryLevel2": "1.1",
"categoryCount": 1
},
{
"productCategoryLevel3": "1.1.3",
"productCategoryLevel3Description": "Product Category 1.1.3",
"productCategoryLevel2": "1.1",
"categoryCount": 2
},
{
"productCategoryLevel3": "1.1.4",
"productCategoryLevel3Description": "Product Category 1.1.4",
"productCategoryLevel2": "1.1",
"categoryCount": 2
}
]
},

Categories is a json object i'm getting from a fetch. This is whats inside categories

 [
    {
    "productCategoryLevel1": "1",
    "productCategoryLevel1Description": "Cartons",
    "sortingKeyLevel1": 1,
    "children": [
    {
    "productCategoryLevel2": "1.1",
    "productCategoryLevel2Description": "Product Category 1.1",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.1.1",
    "productCategoryLevel3Description": "Product Category 1.1.1",
    "productCategoryLevel2": "1.1",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "1.1.2",
    "productCategoryLevel3Description": "Product Category 1.1.2",
    "productCategoryLevel2": "1.1",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.1.3",
    "productCategoryLevel3Description": "Product Category 1.1.3",
    "productCategoryLevel2": "1.1",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "1.1.4",
    "productCategoryLevel3Description": "Product Category 1.1.4",
    "productCategoryLevel2": "1.1",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "1.2",
    "productCategoryLevel2Description": "Product Category 1.2",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.2.1",
    "productCategoryLevel3Description": "Product Category 1.2.1",
    "productCategoryLevel2": "1.2",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "1.2.2",
    "productCategoryLevel3Description": "Product Category 1.2.2",
    "productCategoryLevel2": "1.2",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.2.3",
    "productCategoryLevel3Description": "Product Category 1.2.3",
    "productCategoryLevel2": "1.2",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.2.4",
    "productCategoryLevel3Description": "Product Category 1.2.4",
    "productCategoryLevel2": "1.2",
    "categoryCount": 3
    }
    ]
    },
    {
    "productCategoryLevel2": "1.3",
    "productCategoryLevel2Description": "Product Category 1.3",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.3.1",
    "productCategoryLevel3Description": "Product Category 1.3.1",
    "productCategoryLevel2": "1.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.3.2",
    "productCategoryLevel3Description": "Product Category 1.3.2",
    "productCategoryLevel2": "1.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.3.3",
    "productCategoryLevel3Description": "Product Category 1.3.3",
    "productCategoryLevel2": "1.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.3.4",
    "productCategoryLevel3Description": "Product Category 1.3.4",
    "productCategoryLevel2": "1.3",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "1.4",
    "productCategoryLevel2Description": "Product Category 1.4",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.4.1",
    "productCategoryLevel3Description": "Product Category 1.4.1",
    "productCategoryLevel2": "1.4",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.4.2",
    "productCategoryLevel3Description": "Product Category 1.4.2",
    "productCategoryLevel2": "1.4",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.4.3",
    "productCategoryLevel3Description": "Product Category 1.4.3",
    "productCategoryLevel2": "1.4",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.4.4",
    "productCategoryLevel3Description": "Product Category 1.4.4",
    "productCategoryLevel2": "1.4",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "1.5",
    "productCategoryLevel2Description": "Product Category 1.5",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.5.1",
    "productCategoryLevel3Description": "Product Category 1.5.1",
    "productCategoryLevel2": "1.5",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "1.5.2",
    "productCategoryLevel3Description": "Product Category 1.5.2",
    "productCategoryLevel2": "1.5",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.5.3",
    "productCategoryLevel3Description": "Product Category 1.5.3",
    "productCategoryLevel2": "1.5",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "1.5.4",
    "productCategoryLevel3Description": "Product Category 1.5.4",
    "productCategoryLevel2": "1.5",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "1.6",
    "productCategoryLevel2Description": "Product Category 1.6",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.6.1",
    "productCategoryLevel3Description": "Product Category 1.6.1",
    "productCategoryLevel2": "1.6",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.6.2",
    "productCategoryLevel3Description": "Product Category 1.6.2",
    "productCategoryLevel2": "1.6",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "1.6.3",
    "productCategoryLevel3Description": "Product Category 1.6.3",
    "productCategoryLevel2": "1.6",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.6.4",
    "productCategoryLevel3Description": "Product Category 1.6.4",
    "productCategoryLevel2": "1.6",
    "categoryCount": 4
    }
    ]
    },
    {
    "productCategoryLevel2": "1.7",
    "productCategoryLevel2Description": "Product Category 1.7",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.7.1",
    "productCategoryLevel3Description": "Product Category 1.7.1",
    "productCategoryLevel2": "1.7",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "1.7.2",
    "productCategoryLevel3Description": "Product Category 1.7.2",
    "productCategoryLevel2": "1.7",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.7.3",
    "productCategoryLevel3Description": "Product Category 1.7.3",
    "productCategoryLevel2": "1.7",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "1.7.4",
    "productCategoryLevel3Description": "Product Category 1.7.4",
    "productCategoryLevel2": "1.7",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "1.8",
    "productCategoryLevel2Description": "Product Category 1.8",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.8.1",
    "productCategoryLevel3Description": "Product Category 1.8.1",
    "productCategoryLevel2": "1.8",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.8.2",
    "productCategoryLevel3Description": "Product Category 1.8.2",
    "productCategoryLevel2": "1.8",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.8.3",
    "productCategoryLevel3Description": "Product Category 1.8.3",
    "productCategoryLevel2": "1.8",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.8.4",
    "productCategoryLevel3Description": "Product Category 1.8.4",
    "productCategoryLevel2": "1.8",
    "categoryCount": 2
    }
    ]
    }
    ]
    },
    {
    "productCategoryLevel1": "2",
    "productCategoryLevel1Description": "Films",
    "sortingKeyLevel1": 2,
    "children": [
    {
    "productCategoryLevel2": "2.1",
    "productCategoryLevel2Description": "Product Category 2.1",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.1.1",
    "productCategoryLevel3Description": "Product Category 2.1.1",
    "productCategoryLevel2": "2.1",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.1.2",
    "productCategoryLevel3Description": "Product Category 2.1.2",
    "productCategoryLevel2": "2.1",
    "categoryCount": 5
    },
    {
    "productCategoryLevel3": "2.1.3",
    "productCategoryLevel3Description": "Product Category 2.1.3",
    "productCategoryLevel2": "2.1",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.1.4",
    "productCategoryLevel3Description": "Product Category 2.1.4",
    "productCategoryLevel2": "2.1",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "2.2",
    "productCategoryLevel2Description": "Product Category 2.2",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.2.1",
    "productCategoryLevel3Description": "Product Category 2.2.1",
    "productCategoryLevel2": "2.2",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.2.2",
    "productCategoryLevel3Description": "Product Category 2.2.2",
    "productCategoryLevel2": "2.2",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "2.2.3",
    "productCategoryLevel3Description": "Product Category 2.2.3",
    "productCategoryLevel2": "2.2",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.2.4",
    "productCategoryLevel3Description": "Product Category 2.2.4",
    "productCategoryLevel2": "2.2",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "2.3",
    "productCategoryLevel2Description": "Product Category 2.3",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.3.1",
    "productCategoryLevel3Description": "Product Category 2.3.1",
    "productCategoryLevel2": "2.3",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "2.3.2",
    "productCategoryLevel3Description": "Product Category 2.3.2",
    "productCategoryLevel2": "2.3",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.3.3",
    "productCategoryLevel3Description": "Product Category 2.3.3",
    "productCategoryLevel2": "2.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.3.4",
    "productCategoryLevel3Description": "Product Category 2.3.4",
    "productCategoryLevel2": "2.3",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "2.4",
    "productCategoryLevel2Description": "Product Category 2.4",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.4.1",
    "productCategoryLevel3Description": "Product Category 2.4.1",
    "productCategoryLevel2": "2.4",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.4.2",
    "productCategoryLevel3Description": "Product Category 2.4.2",
    "productCategoryLevel2": "2.4",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.4.3",
    "productCategoryLevel3Description": "Product Category 2.4.3",
    "productCategoryLevel2": "2.4",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.4.4",
    "productCategoryLevel3Description": "Product Category 2.4.4",
    "productCategoryLevel2": "2.4",
    "categoryCount": 1
    }
    ]
    },
    {
    "productCategoryLevel2": "2.5",
    "productCategoryLevel2Description": "Product Category 2.5",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.5.1",
    "productCategoryLevel3Description": "Product Category 2.5.1",
    "productCategoryLevel2": "2.5",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "2.5.2",
    "productCategoryLevel3Description": "Product Category 2.5.2",
    "productCategoryLevel2": "2.5",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.5.3",
    "productCategoryLevel3Description": "Product Category 2.5.3",
    "productCategoryLevel2": "2.5",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.5.4",
    "productCategoryLevel3Description": "Product Category 2.5.4",
    "productCategoryLevel2": "2.5",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "2.6",
    "productCategoryLevel2Description": "Product Category 2.6",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.6.1",
    "productCategoryLevel3Description": "Product Category 2.6.1",
    "productCategoryLevel2": "2.6",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.6.2",
    "productCategoryLevel3Description": "Product Category 2.6.2",
    "productCategoryLevel2": "2.6",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.6.3",
    "productCategoryLevel3Description": "Product Category 2.6.3",
    "productCategoryLevel2": "2.6",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "2.6.4",
    "productCategoryLevel3Description": "Product Category 2.6.4",
    "productCategoryLevel2": "2.6",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "2.7",
    "productCategoryLevel2Description": "Product Category 2.7",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.7.1",
    "productCategoryLevel3Description": "Product Category 2.7.1",
    "productCategoryLevel2": "2.7",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.7.2",
    "productCategoryLevel3Description": "Product Category 2.7.2",
    "productCategoryLevel2": "2.7",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.7.3",
    "productCategoryLevel3Description": "Product Category 2.7.3",
    "productCategoryLevel2": "2.7",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.7.4",
    "productCategoryLevel3Description": "Product Category 2.7.4",
    "productCategoryLevel2": "2.7",
    "categoryCount": 3
    }
    ]
    },
    {
    "productCategoryLevel2": "2.8",
    "productCategoryLevel2Description": "Product Category 2.8",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.8.1",
    "productCategoryLevel3Description": "Product Category 2.8.1",
    "productCategoryLevel2": "2.8",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "2.8.2",
    "productCategoryLevel3Description": "Product Category 2.8.2",
    "productCategoryLevel2": "2.8",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.8.3",
    "productCategoryLevel3Description": "Product Category 2.8.3",
    "productCategoryLevel2": "2.8",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.8.4",
    "productCategoryLevel3Description": "Product Category 2.8.4",
    "productCategoryLevel2": "2.8",
    "categoryCount": 3
    }
    ]
    }
    ]
    },
    {},
    {}
    ]

so what I wanna do is filter on cartons for example and get all cartons with the children of carton.

The issue might be: const filteredCategory = categorieArray.filter(d => d.productCategoryLevel1Description = category);

You need to use == or === to check for equality (prefer === to check type as well as value)

also const categorieArray = [categories];

you are creating an array of a single entry - the categories object. If Categories is already an array you can do categories.filter . If categories not an array you need to do some work on before you can use .filter as it exists only on Array types, eg Object.values(catgories).filter()

function getFilteredCategories(category){
    //check next line
    const categorieArray = Json;
    const filteredCategory = categorieArray.filter(d => d.productCategoryLevel1Description == category);
    console.log(filteredCategory);
    return filteredCategory;   
}

This will work! The problem with your code is that you are copying the array , which shallow copies the array , before it is going to filter, the array is being stripped(nested elements aren't being copied), when you use filter it gives you new array so you don't need to run filter on copied array as it doesn't mutates the original array.

just run filter on original array and it will work fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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