Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

vue 封装一个递归 怎么使的方法调用时每次result都为新数组(不是递归的时候)

`

const result =?[]
export function flatten(arr)?{
 for (let i = 0; i < arr.length; i++)?{
 const item = arr[i]
 if (arr[i].childNodes)?{
 result.push(item)
 flatten(item.childNodes)
 } else {
 result.push(item)
 }
 }
 return result
}

`


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
export function flatten(arr, result = []) {
  for (let i = 0; i < arr.length; i++) {
    const item = arr[i];
    if (arr[i].childNodes) {
      result.push(item);
      flatten(item.childNodes, result);
    } else {
      result.push(item);
    }
  }
  return result;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...