最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filtering array of nested arrays object in Typescript - Stack Overflow

programmeradmin0浏览0评论

I am having some trouble to filter nested array data set. For example I have list of data array below:

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

]

Now I want to achieve following result using .map or .filter and considering the condition if the budget property of parent array and child group array matches then it should return only matched object inside group instead all:

[
  {
    "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
        }
    ]
  }

]

So I performed below action but my result didn't match to my expected result above:

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

Here is following result after above code execution:

[
{
    "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
        }
    ]
}

]

I don't have any clue how to filter the nested group array based on the condition where if parent array budget property equals to group.budget property then it should return only that object but not all.

I'll be very grateful for your help. Many thanks in advance.

Note: I am using typescript with angular-2.

I am having some trouble to filter nested array data set. For example I have list of data array below:

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

]

Now I want to achieve following result using .map or .filter and considering the condition if the budget property of parent array and child group array matches then it should return only matched object inside group instead all:

[
  {
    "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
        }
    ]
  }

]

So I performed below action but my result didn't match to my expected result above:

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

Here is following result after above code execution:

[
{
    "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
        }
    ]
}

]

I don't have any clue how to filter the nested group array based on the condition where if parent array budget property equals to group.budget property then it should return only that object but not all.

I'll be very grateful for your help. Many thanks in advance.

Note: I am using typescript with angular-2.

Share Improve this question edited Aug 18, 2017 at 12:38 user663031 asked Aug 18, 2017 at 11:18 tutorialfeedtutorialfeed 1,0353 gold badges12 silver badges24 bronze badges 2
  • Hi .. I suggest to you this library : https://github./kutyel/linq.ts with this you can archive your goal – federico scamuzzi Commented Aug 18, 2017 at 11:24
  • Thanks for your suggestion but may be I can consider this in my next project. – tutorialfeed Commented Aug 18, 2017 at 11:54
Add a ment  | 

1 Answer 1

Reset to default 9
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)
  }
})

EDIT simplification:

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

评论列表(0)

  1. 暂无评论