使用 Typescript 中的条件对象过滤嵌套数组的数组

普拉萨德·卡纳帕蒂

我有如下对象数组(Json 格式)

var datas = [
  {
    "Id": "1",
    // Here 10 fields
    "tests": [
            {
                "id":"1-1",
                "isSelected": true,
            },
            {
                "id":"1-2",
                "isSelected": false,
            },
        ]
  },
  {
    "Id": "2",
    // Here 10 fields
    "tests": [
            {
                "id":"2-1",
                "isSelected": true,
            },
            {
                "id":"2-2",
                "isSelected": true,
            },
        ]
  },
  {
    "Id": "3",
    // Here 10 fields
    "tests": [
            {
                "id":"3-1",
                "isSelected": false,
            },
            {
                "id":"3-2",
                "isSelected": false,
            },
        ]
  }
]

我试过如下

var filteredData = datas.filter( t => t.tests.filter(o => o.isSelected));

让所有的数据测试"isSelected"价值是true,它不工作。有没有办法以下面的格式获取数据。

[
  {
    "Id": "1",
    // Here 10 fields
    "t": [
            {
                "id":"1-1",
                "isSelected": true,
            }
        ]
  },
  {
    "Id": "2",
    // Here 10 fields
    "tests": [
            {
                "id":"2-1",
                "isSelected": true,
            },
            {
                "id":"2-2",
                "isSelected": true,
            },
        ]
  }
]
TJ克劳德

您走在正确的轨道上,但您正在filter从外部filter回调返回内部结果(一个数组)数组是真实的,因此您保留了所有内容。

您需要分两步处理每个外部对象:

  1. 过滤其tests, 和

  2. 如果它tests是空的,则完全过滤掉它

所以:

var filteredData = datas.filter(t => {
  t.tests = t.tests.filter(o => o.isSelected);
  return t.tests.length !== 0;
});

var datas = [
  {
    "Id": "1",
    // Here 10 fields
    "tests": [
            {
                "id":"1-1",
                "isSelected": true,
            },
            {
                "id":"1-2",
                "isSelected": false,
            },
        ]
  },
  {
    "Id": "2",
    // Here 10 fields
    "tests": [
            {
                "id":"2-1",
                "isSelected": true,
            },
            {
                "id":"2-2",
                "isSelected": true,
            },
        ]
  },
  {
    "Id": "3",
    // Here 10 fields
    "tests": [
            {
                "id":"3-1",
                "isSelected": false,
            },
            {
                "id":"3-2",
                "isSelected": false,
            },
        ]
  }
]

var filteredData = datas.filter(t => {
  t.tests = t.tests.filter(o => o.isSelected);
  return t.tests.length !== 0;
});

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

或者如果你真的想要一个简洁的箭头函数:

所以:

var filteredData = datas.filter(t =>
  (t.tests = t.tests.filter(o => o.isSelected)).length !== 0
);

var datas = [
  {
    "Id": "1",
    // Here 10 fields
    "tests": [
            {
                "id":"1-1",
                "isSelected": true,
            },
            {
                "id":"1-2",
                "isSelected": false,
            },
        ]
  },
  {
    "Id": "2",
    // Here 10 fields
    "tests": [
            {
                "id":"2-1",
                "isSelected": true,
            },
            {
                "id":"2-2",
                "isSelected": true,
            },
        ]
  },
  {
    "Id": "3",
    // Here 10 fields
    "tests": [
            {
                "id":"3-1",
                "isSelected": false,
            },
            {
                "id":"3-2",
                "isSelected": false,
            },
        ]
  }
]

var filteredData = datas.filter(t =>
  (t.tests = t.tests.filter(o => o.isSelected)).length !== 0
);

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Typescript中过滤嵌套数组对象的数组

根据Typescript中对象嵌套数组的值进行过滤

Javascript或lodash:使用嵌套数组的值过滤对象数组

根据多级对象值使用条件过滤嵌套数组并更新它们-MongoDB Aggregate + Update

过滤嵌套数组对象以在 FlatList 中使用

如何基于其他数组使用嵌套数组过滤对象数组

Typescript。从嵌套数组中推送数据

在 TypeScript 中推送嵌套数组

使用jmes查询过滤嵌套数组

使用Lodash / Javascript过滤嵌套数组

如何使用Lodash过滤嵌套数组?

如何使用jQuery在JavaScript中循环嵌套数组对象

使用Go访问嵌套数组和对象中的数据

使用Lodash从嵌套数组中删除对象

如果 Typescript 中的数组为空,则从嵌套数组中删除对象

Typescript使用filter()过滤数组

使用 typeScript 过滤多维数组

TypeScript 中嵌套对象的过滤器数组

使用JavaScript使用对象嵌套数组进行对象动态过滤

如何在angular2 / typescript中使用.map或flatmap从嵌套数组中获取特定数据

如何在嵌套数组对象数组的条件下使用 While 循环?

在TypeScript中使用AM / PM的日期之间过滤对象数组

typescript / javascript删除对象嵌套数组中的对象部分重复

在对象的嵌套数组中使用过滤器

在对象的嵌套数组中使用过滤器

使用 TypeScript 从全局数组中获取对象数组列表

如何将带有嵌套数组的JSON对象映射到TypeScript模型中?

使用嵌套数组对象遍历数组的属性

使用对象的嵌套数组值设置状态数组