1

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",
          }
       ],
    ]
1
  • Is there a problem with variableOpts[0][0]? If those objects could be in any inner array, variableOpts.flat().filter(e => e.assets) or variableOpts.filter(e => e.some(f => f.assets)) or variableOpts.find(e => e.some(f => f.assets)) if you just want the first? Could also use every instead of some if all objects should have the key? Hard to tell which one you want. Commented Jan 26, 2022 at 19:55

1 Answer 1

2

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; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.