【问题标题】:Filter by key in 2d or multidimensional array using JS使用JS在二维或多维数组中按键过滤
【发布时间】:2022-01-26 19:53:53
【问题描述】:

我很想通过嵌套在多数组中的数组中的特定键进行过滤。我不断返回我的过滤器不是一个函数。我试图只返回具有对象键“assets”的数组

我的数据数组:

    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"

期望的输出:

    [
          {
             "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]有问题吗?如果这些对象可以在任何内部数组中,variableOpts.flat().filter(e =&gt; e.assets)variableOpts.filter(e =&gt; e.some(f =&gt; f.assets))variableOpts.find(e =&gt; e.some(f =&gt; f.assets)) 如果您只想要第一个?如果所有对象都应该有密钥,也可以使用every 而不是some?很难说你想要哪一个。

标签: javascript arrays filter


【解决方案1】:

您可以通过查看所需属性的嵌套数组来过滤数组。

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

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 2018-09-20
    • 2013-06-23
    • 1970-01-01
    • 2019-09-04
    相关资源
    最近更新 更多