尝试
function findCategory(categoryName) { var trail = []; var found = false; function recurse(categoryAry) { for (var i = 0; i < categoryAry.length; i++) { trail.push(categoryAry[i].category); // Found the category! if ((categoryAry[i].category === categoryName)) { found = true; break; // Did not match... } else { // Are there children / sub-categories? YES if (categoryAry[i].children.length > 0) { recurse(categoryAry[i].children); if(found){ break; } } } trail.pop(); } } recurse(catalog); return trail}
演示:小提琴
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)