0
我需要找出此深度嵌套數組中的哪個對象包含字符串Toys & Games
(嵌套中最深的對象)。Underscore.js:在深度嵌套對象中匹配字符串
我試圖選擇具有合適的對象:
var arr = _.filter(data, function(obj) {
return _.some(obj.Ancestors, {'Name': "Toys & Games"});
});
其中data
在傳遞給.filter
是:
{
"BrowseNodeId":[
"11608080011"
],
"Name":[
"Quadcopters & Multirotors"
],
"Ancestors":[
{
"BrowseNode":[
{
"BrowseNodeId":[
"166583011"
],
"Name":[
"RC Vehicles"
],
"Ancestors":[
{
"BrowseNode":[
{
"BrowseNodeId":[
"6925830011"
],
"Name":[
"RC Vehicles & Parts"
],
"Ancestors":[
{
"BrowseNode":[
{
"BrowseNodeId":[
"276729011"
],
"Name":[
"Hobbies"
],
"Ancestors":[
{
"BrowseNode":[
{
"BrowseNodeId":[
"165795011"
],
"Name":[
"Categories"
],
"IsCategoryRoot":[
"1"
],
"Ancestors":[
{
"BrowseNode":[
{
"BrowseNodeId":[
"165793011"
],
"Name":[
"Toys & Games"
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"BrowseNodeId":[
"3226142011"
],
"Name":[
"Grown-Up Toys"
],
"Children":[
{
"BrowseNode":[
{
"BrowseNodeId":[
"3226143011"
],
"Name":[
"Action & Toy Figures"
]
},
{
"BrowseNodeId":[
"3226145011"
],
"Name":[
"Die-Cast & Toy Vehicles"
]
},
{
"BrowseNodeId":[
"3226146011"
],
"Name":[
"Games"
]
},
{
"BrowseNodeId":[
"3226148011"
],
"Name":[
"Novelty & Gag Toys"
]
},
{
"BrowseNodeId":[
"3226149011"
],
"Name":[
"Puzzles"
]
},
{
"BrowseNodeId":[
"5483953011"
],
"Name":[
"Executive Desk Toys"
]
}
]
}
],
"Ancestors":[
{
"BrowseNode":[
{
"BrowseNodeId":[
"165795011"
],
"Name":[
"Categories"
],
"IsCategoryRoot":[
"1"
],
"Ancestors":[
{
"BrowseNode":[
{
"BrowseNodeId":[
"165793011"
],
"Name":[
"Toys & Games"
]
}
]
}
]
}
]
}
]
}