简体   繁体   English

Javascript:如何找到元素在多维数组中的位置

[英]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.

相关问题 我可以在JavaScript中选择多维HTML数组作为多维数组吗? - Can I select a multi-dimensional HTML array in JavaScript as a multi-dimensional array? 如何有效创建多维javascript数组? - How do I efficiently create multi-dimensional javascript array? 我如何在打字稿中搜索多维数组中的值 - How can i search for a value in a multi-dimensional array in typescript 如何在JavaScript中填写未定义的锯齿状多维数组? - How can I fill out a jagged multi-dimensional array with undefined in JavaScript? 如何在PHP中创建可以处理JavaScript的多维关联数组? - How do I create a multi-dimensional associative array in PHP which JavaScript can then process? JavaScript多维数组 - JavaScript Multi-Dimensional Array 我们可以从多维数组中更改 javascript 中元素的颜色属性吗? - Can we change color property of an element in javascript from multi-dimensional array? 如何获取一个数组并将其放入另一个数组以在 Javascript 中创建一个多维数组 - How to I take an array and put it into another array to make a multi-dimensional array in Javascript Javascript可以动态创建多维数组吗 - Can Javascript create a multi-dimensional array on the fly 如何从Javascript中的多维数组中查找与当前日期最近的日期 - How to find the closest date from current date from a multi-dimensional array in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM