简体   繁体   English

Javascript/Jquery 数组搜索

[英]Javascript/Jquery array searching

I'm working on an application that uses nested arrays in Javascript. I have an array like this:我正在开发一个在 Javascript 中使用嵌套 arrays 的应用程序。我有一个这样的数组:

var a = ["Once upon a time there was a man."];

    a[1] = ["He was tall."];
           a[1][1] = ["He often bumped his head."];

    a[2] = ["He was short."];
           a[2][1] = ["He couldn't reach high shelves."]

Is there an easy way to figure out what level of an array a value is at?有没有一种简单的方法可以找出值所在的数组级别? I'd like to be able to input "He often bumped his head" and have it return "a[1][1]" (or if I input "He was short" I'd get back "a[2]").我希望能够输入“他经常撞到他的头”并让它返回“a[1][1]”(或者如果我输入“他很矮”我会回来“a[2]” ). The arrays will be of indeterminate and fluctuating size. arrays 将具有不确定和波动的大小。

Thanks in advance for any help with this.在此先感谢您对此的任何帮助。 I'm new to javascript and jquery, so any explanation of your solution will be highly appreciated.我是 javascript 和 jquery 的新手,因此非常感谢您对解决方案的任何解释。 Thanks again.再次感谢。

Some recursion should do the trick:一些递归应该可以解决问题:

Array.prototype.recursiveIndexOf = function(item, start) {
    var i = this.indexOf(item);
    var r, c;

    start = start || [];

    if(i > -1) {
        return start.concat([i]);
    }

    for(i = 0; i < this.length; i++) {
        c = this[i];

        if(Object.prototype.toString.call(c) === '[object Array]') {
            r = c.recursiveIndexOf(item, start.concat(i));

            if(r !== null) {
                return r;
            }
        }
    }

    return null;
};

Here's a demo.这是一个演示。

For a two level array, you could do it like this:对于两级数组,您可以这样做:

function findMultiArray(array, str) {
    var innerArray;
    for (var i = 0; i < array.length; i++) {
        innerArray = array[i];
        for (var j = 0; j < innerArray.length; j++) {
            if (innerArray[j] == str) {
                return([i, j]);
            }
        }
    } 
    return null;
}

Note, I had it just return an actual array with the two indexes since that's a little easier to use if you need the data in data form.请注意,我让它只返回一个包含两个索引的实际数组,因为如果您需要数据形式的数据,它会更容易使用。

Unlike in your request, it will never return the equivalent of a[1] because a[1] is an array, not a string so the match is with a[1][0] , not with a[1] .与您的请求不同,它永远不会返回a[1]的等价物,因为a[1]是一个数组,而不是一个字符串,所以匹配的是a[1][0] ,而不是a[1] In that case, it would return [1,0] .在这种情况下,它将返回[1,0]

If you want the return value in a different form, there's just one line of code where is sees the match and i and j are the indexes that matched.如果你想要不同形式的返回值,只有一行代码可以看到匹配项,i 和 j 是匹配的索引。

If the level of arrays can be arbitrarily deep with different depths in different places, then that would be a little more involved and would require interrogating the type of items to see if they contained nested arrays or just strings and would require some sort of stack to maintain the current position. It would be easiest to do with recursion.如果 arrays 的级别可以任意深,在不同的地方有不同的深度,那么这会涉及更多,并且需要询问项目的类型以查看它们是否包含嵌套的 arrays 或仅包含字符串,并且需要某种堆栈来维护当前的 position。使用递归最容易。

Here is a much simpler answer.这是一个更简单的答案。 ;) ;)

function array_search(ob,str)
{
    for(var i=0;i<ob.length;++i)
    {
        if(typeof(ob[i])=='object'&&(ob[i] instanceof Array))
        {
            var foo=array_search(ob[i],str);
            if(foo!=null)
                return i+'->'+foo;
        }else
        if(typeof(ob[i])=='string'&&ob[i]==str)
        {
            return i;
        }
    }
    return null;
}

var a = ["Once upon a time there was a man."];
a[1] = ["He was tall."];
a[1][1] = ["He often bumped his head."];
a[2] = ["He was short."];
a[2][1] = ["He couldn't reach high shelves."]

alert(array_search(a,'He was short.'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM