利用递归的思想
function findUrl (url, arr) {
for (var i = 0; i < arr.length; i++) {
var item = arr[i]
if (item.url== url) {
return true
break
} else {
if (item.children) {
return this.findUrl(url, item.children)
}
}
}
return false
}
// 一维
findUrl('aa', [{url: 'bb'}, {url: 'cc'}]) // false
findUrl('aa', [{url: 'bb'}, {url: 'aa'}]) // true
// 二维
findUrl('aa', [{url: 'bb', children: [{url: 'dd'}]}, {url: 'cc'}]) // false
findUrl('aa', [{url: 'bb', children: [{url: 'aa'}]}, {url: 'aa'}]) // true
findUrl('aa', [{url: 'bb'}, {url: 'aa', children: [{url: 'aa'}]}]) // true
// 三维
findUrl('aa', [{url: 'bb', children: [{url: 'dd', children: [{url: 'ee'}]}]}, {url: 'cc'}]) // false
findUrl('aa', [{url: 'bb', children: [{url: 'dd', children: [{url: 'aa'}]}]}, {url: 'cc'}]) // true