// 情况一:
// 数据源
var egs = [
{name_1: 'name_1...'},
{name_2: 'name_4...'},
{name_3: 'name_3...'},
{name_0: 'name_0...'},
].reverse()
// 最终想要的数据形式
// {name_1: 'name_1...',
// children: [
// {name_2: 'name_2...',
// children: [
// {name_3: 'name_3...'},
// ]
// }
// ]
// } var result = {}
var key ='children'
s(egs) function s(arrs) {
arrs.forEach((o, i) => {
console.log('---'+ i + ': ', result)
if (!result[key]) {
result[key] = [o]
} else {
o[key] = [result]
result = o
}
})
} console.log(result)
// 情况二:
// pId 对应父级 id
let arr = [
    { pId: '-1', id: '0', name: '父级1' },
    { pId: '0', id: '1', name: '父级1-1' },
    { pId: '0', id: '2', name: '父级1-2' },
    { pId: '2', id: '21', name: '子级21' },
  ]
  function buildTree (arr) {
    let temp = {}
    let tree = {}
    // 数组转 键值对
    arr.forEach(item => {
      temp[item.id] = item
    })
    let tempKeys = Object.keys(temp)
    tempKeys.forEach(key => {
      // 获取当前项
      let item = temp[key]
      // 当前项 pId
      let _itemPId = item.pId
      // 获取父级项
      let parentItemByPid = temp[_itemPId]
      if (parentItemByPid) {
        if (!parentItemByPid.children) {
          parentItemByPid.children = []
        }
        parentItemByPid.children.push(item)
      } else {
        tree[item.id] = item
      }
    })
    // 对象转数组并返回
    return Object.keys(tree).map(key => tree[key])
  }
 
 

js 一维数组,转成嵌套数组的更多相关文章

  1. 如何将PHP对象数组转换成普通数组

    /** * 对象数组转为普通数组 * * AJAX提交到后台的JSON字串经decode解码后为一个对象数组, * 为此必须转为普通数组后才能进行后续处理, * 此函数支持多维数组处理. * * @p ...

  2. 字符串集合或字符串数组转换成json数组

    字符串可以是List<String>类型的字符串集合,也可以是String[]类型的字符串数组,二者转换成JSON数组的方式没有什么不同.下面代码注意关键的部分即可(画红线部分). 1. ...

  3. 批量删除以及将String数组转换成Integer数组的奇淫技巧

    首先在pom.xml文件添加依赖: <!-- bean工具 --> <dependency> <groupId>commons-beanutils</grou ...

  4. ES6将两个数组合并成一个对象数组

    需求 有这么两个数组 let metrodates = [ "2008-01", "2008-02", "2008-03",..ect ]; ...

  5. 【tp5】索引数组转成关联数组 ( $a=[],转换成 $a['aa'=>2,'bb'=>'3c'] )

    概念: 索引数组 ==== >>>$arr = []; 关联数组 ====>>> $arr = [ 'orange'=>1,'apple'=>'good ...

  6. php将对象数组转成普通数组

    不知道为什么,把数组序列化为json,然后存到redis(string类型).然后再取出来反序列化为数组,就变成对象数组了 thinkPHP普通数组取值$arr['key'] 对象数组取值$arr-& ...

  7. 怎样将short[]数组转换成byte[]数组

    byte[] byteArray = Array.ConvertAll<short, byte>(shortArray, Convert.ToByte);

  8. php索引数组转成关联数组

    foreach($revenue_data as $k3=>$v3){ $temps[$v3['_id']['date']]= array( '_id'=>$v3['_id'], 'tot ...

  9. strin 数组转换成int 数组

    string[] strarry = ids.Trim(',').Split(','); int[] arryInts = Array.ConvertAll<string, int>(st ...

随机推荐

  1. 白嫖Office365

    写作不易,资瓷一下呗!个人博客:https://raycoder.me 最近系统升级到1909, 送了我一套Office365. 我也很无奈啊, 送了让我激活也是够了... 用了各种激活软件都无效,比 ...

  2. 一文搞懂 ThreadLocal 原理

    当多线程访问共享可变数据时,涉及到线程间同步的问题,并不是所有时候,都要用到共享数据,所以就需要线程封闭出场了. 数据都被封闭在各自的线程之中,就不需要同步,这种通过将数据封闭在线程中而避免使用同步的 ...

  3. [noip模拟]祖孙询问<LCA>

    [问题描述] 已知一棵n个节点的有根树.有m个询问.每个询问给出了一对节点的编号x和y,询问x与y的祖孙关系. [输入格式] 输入第一行包括一个整数n表示节点个数. 接下来n行每行一对整数对a和b表示 ...

  4. 关于 IDEA 启动 springboot 项目异常 - Disconnected from the target VM, address: '127.0.0.1:59770', transport: 'socket'

    关于 IDEA 启动 springboot 项目异常 - Disconnected from the target VM, address: '127.0.0.1:59770', transport: ...

  5. CodeForces 506B/505D Mr. Kitayuta's Technology

    Portal:http://codeforces.com/problemset/problem/506/B http://codeforces.com/problemset/problem/505/D ...

  6. Ansible Playbook 变量与 register 详解

    ansible 定义变量方式与[多层]变量引用,以及 register 详解 主机规划 添加用户账号 说明: 1. 运维人员使用的登录账号: 2. 所有的业务都放在 /app/ 下「yun用户的家目录 ...

  7. 这个案例写出来,还怕跟面试官扯不明白 OAuth2 登录流程?

    昨天和小伙伴们介绍了 OAuth2 的基本概念,在讲解 Spring Cloud Security OAuth2 之前,我还是先来通过实际代码来和小伙伴们把 OAuth2 中的各个授权模式走一遍,今天 ...

  8. Vue的基本指令的使用1

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Google GMS介绍

    Google GMS介绍GMS全称为GoogleMobile Service.GMS目前提供有Search.Search by Voice.Gmail.Contact Sync.Calendar Sy ...

  10. redis持久化文件问题

    问题: Can't open the append-only file Permission denied 发现缺少文件:/data/缺少appendonly.aof,dump.rdb文件. 手动创建 ...