【问题标题】:Filtering array of nested arrays object in Typescript在 Typescript 中过滤嵌套数组对象的数组
【发布时间】:2018-01-27 01:34:36
【问题描述】:

我在过滤嵌套数组数据集时遇到了一些麻烦。例如,我有下面的数据数组列表:

let list = [
{
  "percentage": 50.0,
  "budget": "online",
  "ruleName": "C1"
},
{
  "percentage": 50.0,
  "budget": "offline",
  "ruleName": "C1"
},
{
  "percentage": 50.0,
  "budget": "other",
  "ruleName": "C4"
}

]

现在我想使用 .map.filter 实现以下结果,并考虑 父数组 的预算属性和 子组数组 匹配,那么它应该只返回 group 内匹配的 object 而不是全部:

[
  {
    "budget": "online",
    "ruleName": "C1",
    "group": [
        {
            "budget": "onlne",
            "percentage": 50.0
        }
    ]
  },

  {
    "budget": "offline",
    "ruleName": "C1",
    "group": [
        {
            "budget": "offline",
            "percentage": 50.0
        }
    ]
  },

  {
    "budget": "other",
    "ruleName": "C4",
    "group": [
        {
            "budget": "other",
            "percentage": 0
        }
    ]
  }

]

所以我执行了以下操作,但结果与上面的预期结果不匹配:

this.group = list.map((i)=>{
  return {
    budget: i.budget,
  }
})
this.payments = list.map((i)=>{
  return {
    budget: i.budget,
    amtPercentage: i.percentage ? i.percentage : 0,
    rulename: i.rulename,
    group: this.group
  }
})

以上代码执行后的结果如下:

[
{
    "budget": "online",
    "ruleName": "C1",
    "group": [
        {
            "budget": "onlne",
            "percentage": 50.0
        },
        {
            "budget": "offline",
            "percentage": 50.0
        },
        {
            "budget": "other",
            "percentage": 0
        }
    ]
},

{
    "budget": "offline",
    "ruleName": "C1",
    "group": [
        {
            "budget": "onlne",
            "percentage": 50.0
        },
        {
            "budget": "offline",
            "percentage": 50.0
        },
        {
            "budget": "other",
            "percentage": 0
        }
    ]
},

{
    "budget": "other",
    "ruleName": "C4",
    "group": [
        {
            "budget": "onlne",
            "percentage": 50.0
        },
        {
            "budget": "offline",
            "percentage": 50.0
        },
        {
            "budget": "other",
            "percentage": 0
        }
    ]
}

]

我不知道如何根据条件过滤嵌套组数组,如果父数组预算属性等于 group.budget 属性,那么它应该只返回该对象而不是全部。

我将非常感谢您的帮助。提前谢谢了。

注意:我正在使用带有 angular-2 的打字稿。

【问题讨论】:

  • 嗨 .. 我向你推荐这个库:https://github.com/kutyel/linq.ts 有了这个你可以归档你的目标
  • 感谢您的建议,但也许我可以在下一个项目中考虑这一点。

标签: javascript arrays angular filter


【解决方案1】:
this.payments = list.map((i)=>{
  return {
    budget: i.budget,
    amtPercentage: i.percentage ? i.percentage : 0,
    rulename: i.rulename,
    group: this.group.filter((x) => i.budget === x.budget)
  }
})

编辑简化:

this.payments = list.map((listElement) => ({
    ...listElement,
    amtPercentage: listElement.percentage || 0,
    group: this.group.filter((groupElement) => listElement.budget === groupElement.budget)
}))

【讨论】:

  • 感谢您的解决方案,但我将组作为空数组。见下文:[{“预算”:“在线”,“规则名称”:“C1”,“组”:[]},{“预算”:“离线”,“规则名称”:“C1”,“组”: [] }, {“预算”:“其他”,“规则名称”:“C4”,“组”:[] }]
  • 那么this.group 是空的吗?过滤器只是从this.group 中挑选元素,其中budget 值作为当前元素(i)。
  • 是的,正确,它应该如何工作,但仍然是空的,我不知道为什么?
  • 它现在可以工作了......这是我没有注意到的愚蠢的错字。再次感谢您挽救了我的一天。干杯!!!
  • 没问题:) 请随意单击我的答案旁边的复选标记 (✓) 以接受它作为正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-08
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 2020-03-11
  • 2019-08-27
  • 1970-01-01
相关资源
最近更新 更多