I am trying to filter a multidimensional array with the inner array containing an object. Every post I have found on here looks like the example below, and all the solutions were pretty much the same and were not successful for me.
let arr = [ { name: 'brent', age: 123 } ];
This is closest I can get, I can console log the filter and see it filtering, but I cannot find out why my array is returning undefined.
let filteredData = [];
let responseData.results = [...]; //The contents of this array are below in the next block
filteredData = responseData.results.forEach((group) =>
{
group.filter(({ customerName }) => customerName.includes(searchTerm));
});
console.log(filteredData); //returns as undefined?
let responseData.results = [
[
{
"id": "e62d6610-04aa-4e92-806f-0449ab3becad",
"customerName": "Immunics",
"enduserName": "Everest",
"productId": 4,
"productName": "fugiat",
"createdBy": "Keith",
"created": "2013-10-31",
"expires": "2011-03-03"
},
{
"id": "3be2967d-cd92-488b-97e3-a8001da6d1c5",
"customerName": "Techtrix",
"enduserName": "Lunchpad",
"productId": 2,
"productName": "sit",
"createdBy": "Odessa",
"created": "2010-02-25",
"expires": "2011-05-22"
},
{
"id": "a67861ae-4e1b-4637-af45-096d6ed7a50f",
"customerName": "Kaggle",
"enduserName": "Strezzo",
"productId": 4,
"productName": "consequat",
"createdBy": "Matilda",
"created": "2010-09-05",
"expires": "2011-08-01"
},
{
"id": "182f84d5-fc27-414c-b011-0e26a9b8e688",
"customerName": "Idealis",
"enduserName": "Blurrybus",
"productId": 0,
"productName": "veniam",
"createdBy": "Merle",
"created": "2012-02-02",
"expires": "2014-05-22"
},
{
"id": "cc05e705-3b5f-4fe6-8c6e-1873fd3834ac",
"customerName": "Furnigeer",
"enduserName": "Harmoney",
"productId": 5,
"productName": "anim",
"createdBy": "Humphrey",
"created": "2010-08-15",
"expires": "2012-06-06"
}
],
[
{
"id": "f2d0ff8e-31ad-4415-8089-cce9df68d119",
"customerName": "Neocent",
"enduserName": "Inear",
"productId": 3,
"productName": "duis",
"createdBy": "Christian",
"created": "2010-01-23",
"expires": "2013-05-18"
},
{
"id": "485095fa-ac86-475c-9a7c-ad449334060d",
"customerName": "Digiprint",
"enduserName": "Bristo",
"productId": 0,
"productName": "occaecat",
"createdBy": "Moran",
"created": "2010-09-30",
"expires": "2014-06-14"
},
{
"id": "d3cacece-1f96-4055-8672-4e506b73e437",
"customerName": "Nexgene",
"enduserName": "Viasia",
"productId": 7,
"productName": "exercitation",
"createdBy": "John",
"created": "2012-07-23",
"expires": "2014-03-31"
},
{
"id": "7fe019e3-7175-48a0-9e2d-1681ce1bfb64",
"customerName": "Quilm",
"enduserName": "Hawkster",
"productId": 3,
"productName": "do",
"createdBy": "Stevens",
"created": "2014-04-28",
"expires": "2013-07-27"
},
{
"id": "7c83a2ea-aa43-4413-af86-02fcd20740e3",
"customerName": "Zillacon",
"enduserName": "Zanity",
"productId": 5,
"productName": "dolor",
"createdBy": "Cathryn",
"created": "2010-07-05",
"expires": "2011-11-26"
}
],
[
{
"id": "2fc29e02-ae0c-413f-ba5a-270a8ba73e5a",
"customerName": "Zepitope",
"enduserName": "Cujo",
"productId": 4,
"productName": "quis",
"createdBy": "Aurora",
"created": "2013-03-22",
"expires": "2014-09-27"
},
{
"id": "07020c5e-7512-456b-af77-3f95d3b8724b",
"customerName": "Melbacor",
"enduserName": "Elemantra",
"productId": 0,
"productName": "amet",
"createdBy": "Pitts",
"created": "2010-09-06",
"expires": "2014-08-14"
},
{
"id": "be9ecd9d-7060-43a2-83b8-b05c52954b9c",
"customerName": "Combot",
"enduserName": "Circum",
"productId": 7,
"productName": "ex",
"createdBy": "Mindy",
"created": "2013-07-05",
"expires": "2014-07-26"
},
{
"id": "991a986b-bd3b-426a-9fc8-113a312c4a19",
"customerName": "Canopoly",
"enduserName": "Accusage",
"productId": 3,
"productName": "proident",
"createdBy": "Cohen",
"created": "2014-02-09",
"expires": "2011-03-17"
},
{
"id": "d843e391-c56d-4dc4-a1d1-dc1b936bc9bd",
"customerName": "Mobildata",
"enduserName": "Eargo",
"productId": 4,
"productName": "culpa",
"createdBy": "Kathie",
"created": "2013-04-03",
"expires": "2012-07-03"
}
],
[
{
"id": "940c6df9-0bbb-43d0-a463-23ba6754a0bb",
"customerName": "Signity",
"enduserName": "Bovis",
"productId": 8,
"productName": "occaecat",
"createdBy": "Monroe",
"created": "2013-09-03",
"expires": "2014-03-23"
},
{
"id": "7479851f-8bba-448b-9af4-75076d81c61f",
"customerName": "Zilladyne",
"enduserName": "Mazuda",
"productId": 8,
"productName": "voluptate",
"createdBy": "Bertie",
"created": "2012-05-03",
"expires": "2010-09-11"
},
{
"id": "dda601dc-6671-43ca-847f-53a90e61f123",
"customerName": "Farmex",
"enduserName": "Greeker",
"productId": 7,
"productName": "eiusmod",
"createdBy": "Jody",
"created": "2010-06-17",
"expires": "2010-09-19"
},
{
"id": "dee1e763-bc49-4b14-87c9-41c7dee01763",
"customerName": "Fibrodyne",
"enduserName": "Besto",
"productId": 7,
"productName": "ex",
"createdBy": "Arlene",
"created": "2012-01-29",
"expires": "2013-10-14"
},
{
"id": "89d69c32-c801-469f-b40f-3d8c85184b08",
"customerName": "Sustenza",
"enduserName": "Omatom",
"productId": 9,
"productName": "consectetur",
"createdBy": "Pruitt",
"created": "2011-03-24",
"expires": "2014-09-13"
}
],
[
{
"id": "2a1d82fc-d7df-4fde-bd4c-97fabccf217e",
"customerName": "Essensia",
"enduserName": "Comverges",
"productId": 8,
"productName": "aliquip",
"createdBy": "White",
"created": "2010-06-23",
"expires": "2014-04-02"
},
{
"id": "f16e04f8-8431-43ac-8315-9822435fe9b0",
"customerName": "Accruex",
"enduserName": "Gluid",
"productId": 6,
"productName": "veniam",
"createdBy": "Kim",
"created": "2010-07-14",
"expires": "2010-04-09"
},
{
"id": "47728404-a86a-4f91-a4fb-d54c6da88a74",
"customerName": "Miracula",
"enduserName": "Gonkle",
"productId": 2,
"productName": "proident",
"createdBy": "Moon",
"created": "2013-07-08",
"expires": "2010-09-10"
},
{
"id": "96810031-64e0-4565-ae0f-308db3fbf7eb",
"customerName": "Viocular",
"enduserName": "Toyletry",
"productId": 3,
"productName": "sit",
"createdBy": "Callahan",
"created": "2014-10-28",
"expires": "2011-01-11"
},
{
"id": "fd223cf3-7f1b-4afb-bccf-cf1907a33334",
"customerName": "Genekom",
"enduserName": "Chorizon",
"productId": 3,
"productName": "ipsum",
"createdBy": "Calhoun",
"created": "2013-09-12",
"expires": "2011-02-27"
}
]
]
CodePudding user response:
You have two common ways of going about this.
Either you can do a map
:
const filteredData = responseData.results.map((group) => {
return group.filter(({ customerName }) => customerName.includes(searchTerm));
});
Or even shorter with the implicit inline return:
const filteredData = responseData.results.map((group) =>
group.filter(({ customerName }) => customerName.includes(searchTerm))
);
Or a reduce
to get rid of the non-filtered objects that provide a hit from responseData
.
const filteredData = responseData.results.reduce((result, group) => {
const match = group.filter(({ customerName }) =>
customerName.includes(searchTerm)
);
if (match) result.push(match);
return result;
}, []);
More information:
CodePudding user response:
It's a variable naming mistake. You haven't changed filteredData
. Rename filteredLicenses
to filteredData
and it should work.
CodePudding user response:
You may use reduce
method to filter each group (sub array) and push those filtered groups to the resulting array if the filtered group has at least one item. Using map
method will push empty groups to the resulting array and that's why reduce
should be used to prevent that.
Here's a live demo:
const results = [
[{
"id": "e62d6610-04aa-4e92-806f-0449ab3becad",
"customerName": "Immunics",
"enduserName": "Everest",
"productId": 4,
"productName": "fugiat",
"createdBy": "Keith",
"created": "2013-10-31",
"expires": "2011-03-03"
},
{
"id": "3be2967d-cd92-488b-97e3-a8001da6d1c5",
"customerName": "Techtrix",
"enduserName": "Lunchpad",
"productId": 2,
"productName": "sit",
"createdBy": "Odessa",
"created": "2010-02-25",
"expires": "2011-05-22"
},
{
"id": "a67861ae-4e1b-4637-af45-096d6ed7a50f",
"customerName": "Kaggle",
"enduserName": "Strezzo",
"productId": 4,
"productName": "consequat",
"createdBy": "Matilda",
"created": "2010-09-05",
"expires": "2011-08-01"
},
{
"id": "182f84d5-fc27-414c-b011-0e26a9b8e688",
"customerName": "Idealis",
"enduserName": "Blurrybus",
"productId": 0,
"productName": "veniam",
"createdBy": "Merle",
"created": "2012-02-02",
"expires": "2014-05-22"
},
{
"id": "cc05e705-3b5f-4fe6-8c6e-1873fd3834ac",
"customerName": "Furnigeer",
"enduserName": "Harmoney",
"productId": 5,
"productName": "anim",
"createdBy": "Humphrey",
"created": "2010-08-15",
"expires": "2012-06-06"
}
],
[{
"id": "f2d0ff8e-31ad-4415-8089-cce9df68d119",
"customerName": "Neocent",
"enduserName": "Inear",
"productId": 3,
"productName": "duis",
"createdBy": "Christian",
"created": "2010-01-23",
"expires": "2013-05-18"
},
{
"id": "485095fa-ac86-475c-9a7c-ad449334060d",
"customerName": "Digiprint",
"enduserName": "Bristo",
"productId": 0,
"productName": "occaecat",
"createdBy": "Moran",
"created": "2010-09-30",
"expires": "2014-06-14"
},
{
"id": "d3cacece-1f96-4055-8672-4e506b73e437",
"customerName": "Nexgene",
"enduserName": "Viasia",
"productId": 7,
"productName": "exercitation",
"createdBy": "John",
"created": "2012-07-23",
"expires": "2014-03-31"
},
{
"id": "7fe019e3-7175-48a0-9e2d-1681ce1bfb64",
"customerName": "Quilm",
"enduserName": "Hawkster",
"productId": 3,
"productName": "do",
"createdBy": "Stevens",
"created": "2014-04-28",
"expires": "2013-07-27"
},
{
"id": "7c83a2ea-aa43-4413-af86-02fcd20740e3",
"customerName": "Zillacon",
"enduserName": "Zanity",
"productId": 5,
"productName": "dolor",
"createdBy": "Cathryn",
"created": "2010-07-05",
"expires": "2011-11-26"
}
],
[{
"id": "2fc29e02-ae0c-413f-ba5a-270a8ba73e5a",
"customerName": "Zepitope",
"enduserName": "Cujo",
"productId": 4,
"productName": "quis",
"createdBy": "Aurora",
"created": "2013-03-22",
"expires": "2014-09-27"
},
{
"id": "07020c5e-7512-456b-af77-3f95d3b8724b",
"customerName": "Melbacor",
"enduserName": "Elemantra",
"productId": 0,
"productName": "amet",
"createdBy": "Pitts",
"created": "2010-09-06",
"expires": "2014-08-14"
},
{
"id": "be9ecd9d-7060-43a2-83b8-b05c52954b9c",
"customerName": "Combot",
"enduserName": "Circum",
"productId": 7,
"productName": "ex",
"createdBy": "Mindy",
"created": "2013-07-05",
"expires": "2014-07-26"
},
{
"id": "991a986b-bd3b-426a-9fc8-113a312c4a19",
"customerName": "Canopoly",
"enduserName": "Accusage",
"productId": 3,
"productName": "proident",
"createdBy": "Cohen",
"created": "2014-02-09",
"expires": "2011-03-17"
},
{
"id": "d843e391-c56d-4dc4-a1d1-dc1b936bc9bd",
"customerName": "Mobildata",
"enduserName": "Eargo",
"productId": 4,
"productName": "culpa",
"createdBy": "Kathie",
"created": "2013-04-03",
"expires": "2012-07-03"
}
],
[{
"id": "940c6df9-0bbb-43d0-a463-23ba6754a0bb",
"customerName": "Signity",
"enduserName": "Bovis",
"productId": 8,
"productName": "occaecat",
"createdBy": "Monroe",
"created": "2013-09-03",
"expires": "2014-03-23"
},
{
"id": "7479851f-8bba-448b-9af4-75076d81c61f",
"customerName": "Zilladyne",
"enduserName": "Mazuda",
"productId": 8,
"productName": "voluptate",
"createdBy": "Bertie",
"created": "2012-05-03",
"expires": "2010-09-11"
},
{
"id": "dda601dc-6671-43ca-847f-53a90e61f123",
"customerName": "Farmex",
"enduserName": "Greeker",
"productId": 7,
"productName": "eiusmod",
"createdBy": "Jody",
"created": "2010-06-17",
"expires": "2010-09-19"
},
{
"id": "dee1e763-bc49-4b14-87c9-41c7dee01763",
"customerName": "Fibrodyne",
"enduserName": "Besto",
"productId": 7,
"productName": "ex",
"createdBy": "Arlene",
"created": "2012-01-29",
"expires": "2013-10-14"
},
{
"id": "89d69c32-c801-469f-b40f-3d8c85184b08",
"customerName": "Sustenza",
"enduserName": "Omatom",
"productId": 9,
"productName": "consectetur",
"createdBy": "Pruitt",
"created": "2011-03-24",
"expires": "2014-09-13"
}
],
[{
"id": "2a1d82fc-d7df-4fde-bd4c-97fabccf217e",
"customerName": "Essensia",
"enduserName": "Comverges",
"productId": 8,
"productName": "aliquip",
"createdBy": "White",
"created": "2010-06-23",
"expires": "2014-04-02"
},
{
"id": "f16e04f8-8431-43ac-8315-9822435fe9b0",
"customerName": "Accruex",
"enduserName": "Gluid",
"productId": 6,
"productName": "veniam",
"createdBy": "Kim",
"created": "2010-07-14",
"expires": "2010-04-09"
},
{
"id": "47728404-a86a-4f91-a4fb-d54c6da88a74",
"customerName": "Miracula",
"enduserName": "Gonkle",
"productId": 2,
"productName": "proident",
"createdBy": "Moon",
"created": "2013-07-08",
"expires": "2010-09-10"
},
{
"id": "96810031-64e0-4565-ae0f-308db3fbf7eb",
"customerName": "Viocular",
"enduserName": "Toyletry",
"productId": 3,
"productName": "sit",
"createdBy": "Callahan",
"created": "2014-10-28",
"expires": "2011-01-11"
},
{
"id": "fd223cf3-7f1b-4afb-bccf-cf1907a33334",
"customerName": "Genekom",
"enduserName": "Chorizon",
"productId": 3,
"productName": "ipsum",
"createdBy": "Calhoun",
"created": "2013-09-12",
"expires": "2011-02-27"
}
]
],
/**
* a function that filters the "results" array based on a "searchTerm"
*/
filterResults = searchTerm => results.reduce((r, group) => {
const f = group.filter(({customerName}) => customerName.includes(searchTerm));
/** only push NON empty groups (sub arrays) to the final array */
f.length && r.push(f);
return r;
}, []);
/** test "fileterResults" function */
console.log(filterResults("Farmex"));
.as-console-wrapper {
max-height: 100%!important;
}