Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取元素在大数组[x][y][z]javascript中的位置_Javascript_Jquery_Arrays_Loops - Fatal编程技术网

获取元素在大数组[x][y][z]javascript中的位置

获取元素在大数组[x][y][z]javascript中的位置,javascript,jquery,arrays,loops,Javascript,Jquery,Arrays,Loops,我在上面坐了一个多小时,可能是因为时间太晚了,也可能是我太傻了,但我做不到。 我有两个数组 a[0] = [['','',''],['','',''],['','','']]; a[1] = [['','',''],['','',''],['','','']]; a[2] = [['','',''],['','',''],['','','']]; a[3] = [['','',''],['','',''],['','','']]; b=['','','','', '','','','','',

我在上面坐了一个多小时,可能是因为时间太晚了,也可能是我太傻了,但我做不到。 我有两个数组

a[0] = [['','',''],['','',''],['','','']];
a[1] = [['','',''],['','',''],['','','']];
a[2] = [['','',''],['','',''],['','','']];
a[3] = [['','',''],['','',''],['','','']];

b=['','','','',
'','','','','',
'','','','','',
'','','','','',
'','','','','',
'','','','','',
'','','','','',
'','']
正如您所看到的,数组的格式不同,但是数组a[]中的每个单元格都引用数组b[]中的his兄弟

我试着写一个函数,得到数组a[]中单元格的地址,就像计算(3,2,2)那样 并返回数组b[]中单元格的地址

这是我到目前为止写的

function timecalculating(x,y,z) {
    var count =z;

    var prew=y-1;

    for (var i=k; i>-1; i--) {

        for (var j=vocabulary[i].length; j>-1; j--) {



            count+=vocabulary[i][prew].length;
        }
    }
    alert (count) ;
}
但是我知道这是不对的。。有什么建议吗

以下是一些例子: 输入(3,1,1),表示

a[0] = [['','',''],['','',''],['','','']];
a[1] = [['','',''],['','',''],['','','']];
a[2] = [['','',''],['','',''],['','','']];
a[3] = [['','',''],['','HERE',''],['','','']];

所以它应该把所有的都记下来。(2+3)+(3+3+3)+(3+3+3)+(3+3+3)=32我不确定我是否理解正确,但你的意思是

function timecalculating(x,y,z) {
    var index = 9 * x + 3 * y + z;
    return b[index];
}
编辑 好的,在看到你编辑的问题后,我更好地理解了它

广义解出人意料地复杂,而且涉及递归

试试这个:

function countUp(arr, stopAt, progress, level) {
    //Initialize level and progress (at level 0).
    level = level || 0;
    if(level == 0) {
        if(!progress) {
            progress = [];
            for(var i=0; i<stopAt.length; i++) progress[i] = 0;
        };
        try {
            var ar = arr, val;
            for(var i=0; i<stopAt.length; i++) {
                val = ar[stopAt[i]];
                if( val == undefined || (i<stopAt.length-1 && !$.isArray(val)) ) {
                    throw('');
                }
                ar = val;
            };
        }
        catch(e) {
            return -1;//return -1 if the requested stopAt position doesn't exist.
        }
    }

    progress[level] = 0;
    var c, stop = false;
    for(var i=0, n=0; i<arr.length; i++, progress[level]++) {
        if($.isArray(arr[i])) {
            c = countUp(arr[i], stopAt, progress, level+1);
            n += c.n;
            stop = c.stop;
        }
        else {
            n += 1;
            stop = arrayCompare(stopAt, progress);
        };
        if(stop) break;
    }
    return (level>0) ? {n:n, 'stop':stop} : n-1;
}

//Utility function for comparing two arrays
function arrayCompare(arr1, arr2) {
    if(arr1.length !== arr2.length) return false;
    for(var i=0; i<arr1.length; i++) {
        if(arr1[i] !== arr2[i]) return false;
    }
    return true;
}
其中,
a
是一个数组,第二个参数表示停止位置的索引

如果请求的停止位置不存在,则函数返回-1


对于相同大小的数组,将多维索引转换为一维索引的一般解决方案需要数组中除第一个维度以外的每个维度的维度大小,这取决于您是否使用

在这里的示例中,我将使用行主顺序,因为我认为这正是您需要的(转换为列主顺序很容易)。我将使用下面的示例解释行主索引。考虑下面的数组。

var array = [[0, 1, 2],
             [0, 1, 2],
             [0, 1, 2]];
按照行的主要顺序,我们正在寻找行的索引,就好像它们是一个接一个地组织的一样。这就好像每一行都被追加到一个数组
[0,1,2,0,1,2,0,1,2]
。第一行的索引完全相同(0、1和2)。对于第二行,我们必须加上第一行的长度,即3,给我们指数3,4,5。第一行的长度当然是原始数组第二维度的大小。对于第三行,我们必须将原始数组的第二维度增加两倍,得到6,7,8。这里的模式应该是显而易见的,并在下面的示例中使用

通常,对于大小为
c[height][width]
的二维数组
c
,对应于二维位置
c[y][x]
的一维索引为
y*width+x

对于大小为
d[深度][高度][宽度]
的三维数组
d
,对应于位置
d[z][y][x]
的1D索引为
z*(宽度*高度)+y*宽度+z

同样的概念也适用于不同尺寸的多维阵列。但是,由于子阵列的大小未知,因此不可能像以前那样在一次计算中得到答案。相反,您必须计算给定目标索引之前的元素数。这非常简单,正如您应该在下面的代码中看到的,您可以用它进行测试

/*对给定数组的每个子数组中的元素数求和。
返回结果和。
数组:要对其元素进行计数的数组*/
函数计数(数组){
var元素=0;
if(Array.isArray(Array)){
对于(变量i=0;i
您将看到,我提供了一个函数,
countallegements
,该函数对具有任意嵌套级别的数组中的所有元素进行计数,另一个函数,
countElementsBeforeIndex
,该函数进行计数,直到它达到提供的索引为止。后者使用前者

我希望这有帮助!

如果您给出一个输入和预期输出的示例,“此处”位于错误的位置,这将很有帮助。在[3][1][1]而不是[3][2][2]。你说得对。。我修复了我在
(2+3)+(3+3+3)+(3+3+3)+(3+3+3)=32方面遇到的困难。这些数字来自哪里?请参阅更新。希望现在更好不……:)这只是一个私有的解决方案,我希望它是通用的。。对于像[0]=[['','','','','','',']['','','']这样的数组;a[1]=[['',['','',['',''];a[2]=[['','','','','.['',''];a[3]=[['','',''];
var index = countUp(a, [3,1,1]);
var array = [[0, 1, 2],
             [0, 1, 2],
             [0, 1, 2]];
/*  Sums the number of elements in each sub-array of the given array.
    Returns the resulting sum.
    array: the array to count the elements of. */
function countAllElements(array) {
    var elements = 0;

    if (Array.isArray(array)) {
        for (var i = 0; i < array.length; ++i) {
            elements += countAllElements(array[i]);
        }
    }
    else {
        ++elements;
    }

    return elements;
}


/*  Sums the number of elements in each sub-array of the given array up to the given target index.
    Returns the resulting sum.
    array: the array to count the elements of.
    target: the target indices as an array, e.g. [3, 1, 5].
    depth: current depth in the nested arrays. Do not call with this. */
function countElementsBeforeIndex(array, target, depth) {
    var elements = 0;

    depth = depth || 0;

    if (Array.isArray(array)) {
        for (var i = 0; i < target[depth]; ++i) {
            elements += countAllElements(array[i]);
        }

        elements += countElementsBeforeIndex(array[target[depth]], target, depth + 1, i);
    }

    return elements;
}