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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743780959a4505788.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信