[英]Javascript:How can I find the position of element in the multi-dimensional array
I have a multi array like this: 我有一个像这样的多数组:
var tree=["root",null,["es1",null,["es11",null,"info for es11","param for es11],["es12",null,"info for es12","param for es12]],["es2",null,["es21",null,"info for es21","param for es21],["es22",null,"info for es22","param for es22]]] var tree = [“ root”,null,[“ es1”,null,[“ es11”,null,“ es11的信息”,“ es11的参数],[” es12“,null,” es12的信息“,” es12的参数]],[“ es2”,null,[“ es21”,null,“ es21的信息”,“ es21的参数],[” es22“,null,” es22的信息“,” es22的参数] ]]
If i want search for " es22 ", how can i get the position like tree[3][3][0]
? 如果我想搜索“ es22 ”,我如何获得像tree[3][3][0]
? I have tried like this: 我已经这样尝试过:
function recursion(obj,strs){
if (found) return;
for(var j=0;j<obj.length;j++){
c++;
if (isArray(obj[j])&&!found) {
recursion(obj[j],strs);
} else {
if (obj[j]==strs&&!found) {
arr=obj;
found=true;
return;
}
}
}
Whenever the item is found, return the index
, and append all previous indexes before it: 只要找到该项目,就返回index
,并在其之前附加所有先前的索引:
var tree=["root",null,["es1",null,["es11",null,"info for es11","param for es11"],["es12",null,"info for es12","param for es12"]],["es2",null,["es21",null,"info for es21","param for es21"],["es22",null,"info for es22","param for es22"]]]; function recursion(arr, str, indexes) { var result; for (var i = 0; i < arr.length; i++) { if (Array.isArray(arr[i])) { result = recursion(arr[i], str, indexes); if(result !== null) { return [i].concat(result); } } else if(arr[i] === str) { return i; } } return null; } console.log(recursion(tree, "es22", []));
This is my attempt... 这是我的尝试
var tree = [ "root", null, ["es1", null, ["es11"], ["es12"]], ["es2", null, ["es21"], ["es22"]] ]; const find = (subtree, item, path = []) => Array.isArray(subtree) ? subtree.some((e, i) => (find(e, item, path) && path.unshift(i))) && path : subtree === item; console.log(find(tree, 'es22'));
Description: 描述:
A depth first search. 深度优先搜索。 If subtree
is an array, then enumerate. 如果subtree
是一个数组,则枚举。 For each element, run the depth first search on that subtree. 对于每个元素,在该子树上运行深度优先搜索。
If subtree
IS item
return true
. 如果subtree
IS item
返回true
。 This will cause the stack to unwind. 这将导致堆栈展开。 At each stack frame, if the subtree search was successful, add current array index to the front of path
. 在每个堆栈帧处,如果子树搜索成功,则将当前数组索引添加到path
的前面。
Pass path
up the stack frame chain if item
was found in the subtree. 如果在子树中找到了item
则沿堆栈框架链向上传递path
。
When complete, if the element was found, path
is returned containing indices to the element, otherwise false is returned. 完成后,如果找到了元素,则返回包含元素索引的path
,否则返回false。
Pseudocode: 伪代码:
def solution(subtree, item, path)
if subtree is not an array
return subtree is item
else
for each index, value in subtree
var found = solution(value, item, path)
if found
add index to path
return path
end if
end for
end if
end def
Hope it helps: 希望能帮助到你:
The imeplemntation: 体现:
function findPosition(search, neddle) { for (let i = 0; i < search.length; i++) { if (search[i] === neddle) { return [i]; } else if (Array.isArray(search[i])) { const match = findPosition(search[i], neddle); if (match.length > 0) { return [i].concat(match); } } } return []; }
And the test: 和测试:
// findPosition TEST const tree = [ "root", null, [ "es1", null, ["es11", null, "info for es11", "param for es11"], ["es12", null, "info for es12", "param for es12"] ], [ "es2", null, ["es21", null, "info for es21", "param for es21"], ["es22", null, "info for es22", "param for es22"] ] ]; const expected = [3, 3, 0].join(','); const actual = findPosition(tree, 'es22').join(','); if (actual === expected) { console.log('pass'); } else { console.log('fail'); console.log(actual, 'not equal to', expected) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.