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

时间:2017-12-20 11:22:05

标签: javascript arrays multidimensional-array

我有一个像这样的多数组:

  

var tree = [“root”,null,[“es1”,null,[“es11”,null,“es11的信息”,“es13的参数”,[“es12”,null,“es12的信息] “,”esram的param]],[“es2”,null,[“es21”,null,“es21的信息”,“es21的参数”,[“es22”,null,“es22的信息”,“param”对于es22]]]

如果我想搜索“ es22 ”,我怎样才能获得import re num = 12324 re.match(r'(?:(?<!\d)\d{4}(?!\d))', str(num)) 这样的位置? 我试过这样的:

tree[3][3][0]

3 个答案:

答案 0 :(得分:1)

每当找到该项时,返回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", []));

答案 1 :(得分:1)

这是我的尝试......

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'));

说明

深度优先搜索。如果subtree是数组,则枚举。对于每个元素,在该子树上运行深度优先搜索。

如果subtreeitem,则返回true。这将导致堆栈展开。在每个堆栈帧,如果子树搜索成功,则将当前数组索引添加到path的前面。

如果在子树中找到path,则将item向上传递到堆栈框架链。

完成后,如果找到元素,则返回包含元素索引的path,否则返回false。

<强>伪代码:

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

答案 2 :(得分:0)

希望有所帮助:

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 [];
}

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 []; }

测试:

// 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)
}