Skip to content

Nest

格式化数据,将扁平化数据树形化

nest.js
function nested(data, pid = undefined) {
  return data.reduce((r, e) => {
    if (e.parentId == pid) {
      const obj = { ...e }
      const children = nested(data, e.id);
      if (children.length) obj.children = children;
      r.push(obj)
    }

    return r;
  }, [])
}