Today I discovered the array.filter()
method and its associated callback function.
I have an array of objects that looks like this:
var treeAry = [
{"id" : "200", "parent": "#", "type" : "1"},
{"id" : "300", "parent": "#", "type" : "1"},
{"id" : "400", "parent": "#", "type" : "1"},
{"id" : "500", "parent": "300", "type" : "5"},
{"id" : "600", "parent": "300", "type" : "5"},
{"id" : "700", "parent": "400", "type" : "5"},
{"id" : "800", "parent": "400", "type" : "5"},
{"id" : "900", "parent": "200", "type" : "6"},
{"id" : "1000", "parent": "200", "type" : "9"},
{"id" : "1100", "parent": "200", "type" : "8"},
];
I was successfully able to filter for type 5:
filteredAry = [];
filteredAry = treeAry.filter(function(val, index, ary){
/*get all type 5*/
return val.type === '5';
/*get all type 5 or type 1*/
//return val.type === '1' || val.type === '5';
});
console.dir(filteredAry)
Here's my question:
Although I'm able to filter for type 5, I also need Type 5's parents (a child's parent
references the id
, and a parent
can be a #). Essentially, my results should include anything with type 5, and then also record id 300
and 400
. It should not include id 200
.
Should I run some sort of for each loop on filteredAry[]
to find and push the corresponding item in treeAry[]
? Shouldn't I be able to use the index
and ary
arguments to get the parents somehow?
Sorry, I don't know how to do this and would love your help. Just need a push in the right direction, Thank you!
Try this:
var treeAry = [ {"id" : "200", "parent": "#", "type" : "1"}, {"id" : "300", "parent": "#", "type" : "1"}, {"id" : "400", "parent": "#", "type" : "1"}, {"id" : "500", "parent": "300", "type" : "5"}, {"id" : "600", "parent": "300", "type" : "5"}, {"id" : "700", "parent": "400", "type" : "5"}, {"id" : "800", "parent": "400", "type" : "5"}, {"id" : "900", "parent": "200", "type" : "6"}, {"id" : "1000", "parent": "200", "type" : "9"}, {"id" : "1100", "parent": "200", "type" : "8"}, ]; var result = treeAry.filter(value => { return value.type === '5' || treeAry.filter(val => val.parent === value.id && val.type === '5').length > 0; }); console.log(result)
You can replace lambdas (arrow functions) with ordinary functions if ES6 is an issue.
One way would be
var treeAry = [ {"id" : "200", "parent": "#", "type" : "1"}, {"id" : "300", "parent": "#", "type" : "1"}, {"id" : "400", "parent": "#", "type" : "1"}, {"id" : "500", "parent": "300", "type" : "5"}, {"id" : "600", "parent": "300", "type" : "5"}, {"id" : "700", "parent": "400", "type" : "5"}, {"id" : "800", "parent": "400", "type" : "5"}, {"id" : "900", "parent": "200", "type" : "6"}, {"id" : "1000", "parent": "200", "type" : "9"}, {"id" : "1100", "parent": "200", "type" : "8"}, ]; filteredAry = []; filteredAry = treeAry.filter(function(val, index, ary){ /*get all type 5*/ return val.type === '5' || ary.some(function(item){ return item.parent === val.id && item.type==='5'; }); }); console.dir(filteredAry)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.