首页 > Web开发 > 详细

js递归的一些操作

时间:2021-07-09 12:29:31      阅读:18      评论:0      收藏:0      [点我收藏+]
//重新向新的数组里面塞值
function renderListValue(arr, newarr) {
  for (let i = 0; i < arr.length; i++) {
    let obj = {}
    if (arr[i].leaf == true) {
      obj.budgetAmount = arr[i].value
      obj.typeId = arr[i].key
      newarr.push(obj)
    }
    if (arr[i].children && arr[i].children.length > 0) {
      renderListValue(arr[i].children, newarr)
    }
  }
  return newarr
}

//递归赋值

function renderCode(arr, code, text) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].code == code) {
      arr[i].value = text
    }
    if (arr[i].children && arr[i].children.length > 0) {
      renderCode(arr[i].children, code, text)
    }
  }
  return arr
}
//根据id或者key取到父级
 
function findParent(data, code) {
  let result = null
  function setParent(data) {
    data.children.forEach((item, index) => {
      item.index = index
      item.parent = data
      if (item.code === code) {
        result = item
      }
      if (item.children && item.children.length) {
        setParent(item)
      }
    })
  }
  data.forEach((item, index) => {
    item.index = index
    setParent(item)
  })
  return result ? result.parent : null
}

 

 

 

js递归的一些操作

原文:https://www.cnblogs.com/wangshengli520/p/14989319.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!