Home > database >  Filter by key in 2d or multidimensional array using JS
Filter by key in 2d or multidimensional array using JS

Time:01-27

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",
          }
       ],
    ]

CodePudding user response:

You could filter the array by looking into the nested arrays for the wanted property.

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" }]],
    result = variableOpts.filter(a => a.length && a.some(o => 'assets' in o));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  •  Tags:  
  • Related