code: function tree (list) {
    var mapped = {}
    for (var i = 0, item; i < list.length; i++) {
      item = list[i]
      if (!item || !item.id) continue
      mapped[item.id] = item
    }
  
    var result = []
    for (var ii = 0; ii < list.length; ii++) {
      item = list[ii]
  
      if (!item) continue
      /* jshint -W041 */
      if (item.pid == undefined && item.parentId == undefined) {
        result.push(item)
        continue
      }
      var parent = mapped[item.pid] || mapped[item.parentId]
      if (!parent) continue
      if (!parent.children) parent.children = []
      parent.children.push(item)
    }
    return result
  }
docs: id pid/parentId name children