【问题标题】:Finding the position of super nested array of objects查找超级嵌套对象数组的位置
【发布时间】:2019-08-14 09:44:30
【问题描述】:

我正在尝试找到确切的位置并访问超级嵌套对象数组的所有属性。

我正在努力创建一个函数,如果我将索引号作为输入参数,它应该给我它在数组中的位置,并且还可以访问所有属性作为回报。

这是对象的示例数组

我对 ES6 及以上解决方案也很满意

{
	"name": "branch 1",
	"index": 1,
	"children": [{
			"name": "sub child 1",
			"index": 2,
			"children": [{
				"name": "subx2 child 1",
				"index": 3,
				"children": [{
						"name": "subx3 child 1",
						"index": 4,
						"children": [{
								"name": "subx4 child 1",
								"index": 21
							},
							{
								"name": "subx4 child 2",
								"index": 18
							}
						]
					},
					{
						"name": "subx3 child 2",
						"index": 6,
						"children": [{
								"name": "subx4 child 1",
								"index": 7
							},
							{
								"name": "subx4 child 2",
								"index": 21
							}
						]

					},
					{
						"name": "subx3 child 3",
						"index": 22
					}
				]
			}]
		},
		{
			"name": "sub child 2",
			"index": 28
		}
	]
}

是的,我知道这个 json 对象很可怕,需要花时间解决。非常感谢任何形式的帮助。

例如,如果我的函数名称是 findChildIndex(22),它应该返回类似 x.children[0].children[0].children[2]

谢谢!

【问题讨论】:

  • “它应该给我它在数组中的位置,并且还可以访问所有属性作为回报”:请提供输入和预期输出的具体示例。
  • 例如,我的函数名称是 findChildIndex(indexNumber) 例如如果 22 它应该返回我它的位置返回我这个 children[0].children[0].children[2]
  • 我认为你需要问问自己为什么这些数据的结构是这样的。平面数据更适合 json。
  • @JoeLloyd 此数据用于npmjs.com/package/react-d3-tree,所以帮不上忙! :(

标签: javascript arrays reactjs object ecmascript-6


【解决方案1】:

您可以使用递归并检查元素的children 是否存在使用for 循环遍历所有children 并递归应用每个子元素的功能

const obj = {
	"name": "branch 1",
	"index": 1,
	"children": [{
			"name": "sub child 1",
			"index": 2,
			"children": [{
				"name": "subx2 child 1",
				"index": 3,
				"children": [{
						"name": "subx3 child 1",
						"index": 4,
						"children": [{
								"name": "subx4 child 1",
								"index": 21
							},
							{
								"name": "subx4 child 2",
								"index": 18
							}
						]
					},
					{
						"name": "subx3 child 2",
						"index": 6,
						"children": [{
								"name": "subx4 child 1",
								"index": 7
							},
							{
								"name": "subx4 child 2",
								"index": 21
							}
						]

					},
					{
						"name": "subx3 child 3",
						"index": 22
					}
				]
			}]
		},
		{
			"name": "sub child 2",
			"index": 28
		}
	]
}

function find(obj,index){
  if(obj.children){
    for(let i = 0;i<obj.children.length;i++){
      let x = find(obj.children[i],index);
      if(x) return {...x,pos:i};
    }

  }
  return obj.index === index ? obj : false;
}

console.log(find(obj,21))

【讨论】:

  • 感谢您的帮助。这里只有一个问题我怎么知道它存在的索引位置,比如 22 它可能是 Obj.children[0].children[0].children[2]
【解决方案2】:

如果我没有正确回答您的问题,您可以这样做:

const func=(obj,index, nested=0)=>{
    return Obj.index===index ? {obj, nested} : func(obj.children,index, nested+1)
}

【讨论】:

    【解决方案3】:

    您可以递归地收集children 数组中指向目标索引的索引:

    function findIndexNested(data, index) {
        if (data.index === index) return [];
        let result;
        const i = (data.children || []).findIndex(child => {
            return result = findIndexNested(child, index)
        });
        if (result) return [i, ...result];
    }
    function findByPath(data, path) {
        for (let i of path) data = data.children[i];
        return data
    }
    // Sample data
    const data = {"name": "branch 1","index": 1,"children": [{"name": "sub child 1","index": 2,"children": [{"name": "subx2 child 1","index": 3,"children": [{"name": "subx3 child 1","index": 4,"children": [{"name": "subx4 child 1","index": 21},{"name": "subx4 child 2","index": 18}]},{"name": "subx3 child 2","index": 6,"children": [{"name": "subx4 child 1","index": 7},{"name": "subx4 child 2","index": 21}]},{"name": "subx3 child 3","index": 22}]}]},{"name": "sub child 2","index": 28}]}
    const index = 22
    const result = findIndexNested(data, index);
    console.log("Found index " + index + " via these child indexes: " + result);
    console.log("The object is", findByPath(data, result));

    【讨论】:

    • 感谢@trincot 的帮助!我真的很感激。
    • 如何从数组返回 [0,0,2] 形成Obj.children[0].children[0].children[2]?
    • 你的意思是,作为一个字符串?
    • 是的,就像我想将它存储在变量中一样var properties = Obj.children[0].children[0].children[2]。如果可以,请帮助我
    • 等等,那不是字符串。你是说var properties = "Obj.children[0].children[0].children[2]"?
    猜你喜欢
    • 2013-07-08
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 2017-12-24
    • 1970-01-01
    • 2019-09-27
    相关资源
    最近更新 更多