How to get filter nested data and result map to new props name. Please have look at below codesandbox.
https://codesandbox.io/s/es6-filter-sub-data-and-return-new-obj-d9s0o1?file=/src/index.js
Have tried with below approach but its not getting filter for child objects.
console.log(getFilterData("type2"));
function getFilterData(type) {
var r = data.filter((d) => d.subs.some((s) => s.type === type));
console.log(r);
/* expected to return: (only return "type2" filter result with
some new map "prop key" name)
{
id: 1,
name: "name_1",
subModule: [ {
id: "sub_2",
type: "type2"
},
{
id: "sub_3",
type: "type2"
}]
}
*/
}
var data = [
{
id: 1,
name: "name_1",
subs: [
{
id: "sub_1",
name: "sub_1",
type: "type1"
},
{
id: "sub_2",
name: "sub_2",
type: "type2"
},
{
id: "sub_3",
name: "sub_3",
type: "type2"
}
]
},
{
id: 2,
name: "name_2",
subs: [
{
id: "sub_11",
name: "sub_11",
type: "type3"
},
{
id: "sub_21",
name: "sub_21",
type: "type1"
}
]
}
];
You can
mapyourdatato a new array which has objects with thesubsarray filtered ontype, thenfilterthat array by the length of thesubsarray:If you want to change the name of the
subsarray in the process, change themapandfilterto:Also, if you want to remove the name field from subs, change
to
Updated code with both changes: