I am tyring to filter by a particualar key in my array that is nested inside a multi arrary. I keep getting returned my filter is not a function. I am trying to return only the array that has the object key "assets"
My Data Array:
const variableOpts = [
[
{
"assets":"EP009285440323",
"rootId":"21253358",
},
{
"assets":"EP009285440323",
"rootId":"21253358",
},
{
"assets":"EP009285440323",
"rootId":"21253358",
}
],
[
{
"TMSId":"EP035579760050",
"rootId":"21253391",
},
{
"TMSId":"EP035579760050",
"rootId":"21253391",
},
{
"TMSId":"EP035579760050",
"rootId":"21253391",
}
],
[
{
"TMSId":"EP033168400060",
"rootId":"21166708",
},
{
"TMSId":"EP033168400060",
"rootId":"21166708",
},
{
"TMSId":"EP033168400060",
"rootId":"21166708",
}
],
[
{
"assets":"EP00928544",
"rootId":"111",
},
{
"assets":"EP00",
"rootId":"222",
},
{
"assets":"EP00928544]",
"rootId":"444",
}
],
]
JS:
const filResults = variableOpts.map((el) => {
return el.map((prg) => prg.filter((obj) => obj.includes("assets")));
});
console.log("filData", filResults); <---Uncaught TypeError: prg.filter is not a function"
Desired Output:
[
{
"assets":"EP009285440323",
"rootId":"21253358",
},
{
"assets":"EP009285440323",
"rootId":"21253358",
},
{
"assets":"EP009285440323",
"rootId":"21253358",
}
],
[
{
"assets":"EP00928544",
"rootId":"111",
},
{
"assets":"EP00",
"rootId":"222",
},
{
"assets":"EP00928544]",
"rootId":"444",
}
],
]
variableOpts[0][0]? If those objects could be in any inner array,variableOpts.flat().filter(e => e.assets)orvariableOpts.filter(e => e.some(f => f.assets))orvariableOpts.find(e => e.some(f => f.assets))if you just want the first? Could also useeveryinstead ofsomeif all objects should have the key? Hard to tell which one you want.