Javascript Java脚本通过递归获取根元素

Javascript Java脚本通过递归获取根元素,javascript,arrays,json,object,recursion,Javascript,Arrays,Json,Object,Recursion,我需要获取每个节点的根元素。 我有以下两个功能。 当我运行getRoot时,我得到了undefined 我认为问题在于递归函数 例如:对于节点id=3,我需要{id:1,name:“Business1”} const items=[{id:1,名称:“Business1”}, {id:2,名称:“Canton1”,父项:1}, {id:3,名称:“Branch1”,父项:2}, {id:4,名称:“Branch2”,父项:2}, {id:5,名称:“Canton2”,父项:1}, {id:6,

我需要获取每个节点的根元素。
我有以下两个功能。
当我运行
getRoot
时,我得到了
undefined

我认为问题在于递归函数

例如:对于节点
id=3
,我需要
{id:1,name:“Business1”}

const items=[{id:1,名称:“Business1”},
{id:2,名称:“Canton1”,父项:1},
{id:3,名称:“Branch1”,父项:2},
{id:4,名称:“Branch2”,父项:2},
{id:5,名称:“Canton2”,父项:1},
{id:6,名称:“Branch1”,父项:5},
{id:7,名称:“Branch2”,父项:5},
{id:8,姓名:“Canton3”,家长:1},
{id:9,名称:“Branch1”,父项:8},
{id:10,名称:“Branch2”,父项:8},
{id:9,名称:“Business2”},
{id:10,姓名:“Canton1”,家长:9},
{id:11,名称:“Branch1”,父项:10},
{id:12,名称:“Branch2”,父项:10},
{id:13,姓名:“Canton2”,家长:9},
{id:14,名称:“Branch1”,父项:13},
{id:15,名称:“Branch2”,父项:13},
{id:16,姓名:“Canton3”,家长:9},
{id:17,名称:“Branch1”,父项:16},
{id:18,名称:“Branch2”,父项:16}]
const getParent=(id)=>{
const node=items.filter(e=>e.id==id)
如果(node.length==0)返回undefined//则在此处抛出一些错误
设currentNode=node[0]
return currentNode.parent!==未定义?getParent(currentNode.parent):currentNode
}
console.log(getParent(3))
let\u项目=[
{id:1,名称:“Business1”},
{id:2,名称:“Canton1”,父项:1},
{id:3,名称:“Branch1”,父项:2},
{id:4,名称:“Branch2”,父项:2},
{id:5,名称:“Canton2”,父项:1},
{id:6,名称:“Branch1”,父项:5},
{id:7,名称:“Branch2”,父项:5},
{id:8,姓名:“Canton3”,家长:1},
{id:9,名称:“Branch1”,父项:8},
{id:10,名称:“Branch2”,父项:8},
{id:11,名称:“Business2”},
{id:12,姓名:“Canton1”,家长:11},
{id:13,名称:“Branch1”,父项:12},
{id:14,名称:“Branch2”,父项:12},
{id:15,姓名:“Canton2”,家长:11},
{id:16,名称:“Branch1”,父项:15},
{id:17,名称:“Branch2”,父项:15},
{id:18,姓名:“Canton3”,家长:11},
{id:19,姓名:“Branch1”,父项:18},
{id:20,姓名:“Branch2”,父项:18}
];
函数findElementById(id){
让元素_返回=false;
_items.forEach(元素=>{
if(id==element.id){
元素\返回=元素;
返回true;
}
});
返回元素返回;
}
函数getRoot(父级){
元素=findElementById(父元素);
if(元素){
if(typeof(element.parent)!=“未定义”){
返回getRoot(element.parent);
}否则{
返回元素.id;
}
}否则{
返回父母;
}
}
设test_root=getRoot(3);
console.log(test_root);
log(findElementById(test_root));
test_root=getRoot(11);
console.log(test_root);

log(findElementById(test_root))谢谢亲爱的,但是对于id=11,我需要{id:9,name:“Business2”},@Naor Tedginode 10出现两次Branch2和Canton1在这种情况下的策略是什么?您可以进入11->10->8->1,也可以进入11->10->9,对于10到18的所有节点,它们的父节点是{id:9,名称:“Business2”}@Naor Tedgilook请再次查看数据{id:10,名称:“Branch2”,父节点:8},以及{id:10,名称:“Canton1”,父节点:9},同一节点2个不同的父节点是的,它可以解决它,但这并不是Alaa想要的用例我认为对索引有误解亲爱的谢谢,但我并不总是需要返回{id:1,name:“Business1},对于id=11,我需要{id:9,name:“Business2},@angel。bonev@Alaa我添加了11作为测试,它返回
{id:9,name:“Business2”}
@Alaa但您有两个id:10我的方法是遵循第一个找到的元素,您将有与13相同的问题抱歉,亲爱的,id是从1到20的序列@angel。bonev@Alaa你能用正确的输入数据更新你的问题吗?
数组中有两个
id:9
id:10
。你想要哪一个?我建议您编辑
数组
//to get the parent for any node
function getParent(id) {
  const node = this._items.find(
    item => item.parent !== undefined && item.id == id
  );
  return node.parent;
}

//to get the root for the node
function getRoot(parent) {
  console.log(this._items);
  this._items.forEach(element => {
    if (element.id == parent) {
      if (element.parent === undefined) return element;
      else return this.getRoot(element.parent);
    }
  });
}
//constant array
const array = [
  {id: 1, name: "Business1"},
  {id: 2, name: "Canton1", parent: 1},
  {id: 3, name: "Branch1", parent: 2},
  {id: 4, name: "Branch2", parent: 2},
  {id: 5, name: "Canton2", parent: 1},
  {id: 6, name: "Branch1", parent: 5},
  {id: 7, name: "Branch2", parent: 5},
  {id: 8, name: "Canton3", parent: 1},
  {id: 9, name: "Branch1", parent: 8},
  {id: 10, name: "Branch2", parent: 8},
  {id: 9, name: "Business2"},
  {id: 10, name: "Canton1", parent: 9},
  {id: 11, name: "Branch1", parent: 10},
  {id: 12, name: "Branch2", parent: 10},
  {id: 13, name: "Canton2", parent: 9},
  {id: 14, name: "Branch1", parent: 13},
  {id: 15, name: "Branch2", parent: 13},
  {id: 16, name: "Canton3", parent: 9},
  {id: 17, name: "Branch1", parent: 16},
  {id: 18, name: "Branch2", parent: 16},
];