javascript - Filtering array of nested arrays object in Typescript -


i having trouble filter nested array data set. example 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 want achieve following result using .map or .filter , considering condition if budget property of parent array , child group array matches should return 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 performed below action result didn't match 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 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 clue how filter nested group array based on condition if parent array budget property equals group.budget property should return object not all.

i'll grateful help. many in advance.

note: using typescript angular-2.

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

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -