简体   繁体   English

javascript如何将数组中的每个元素传递给不同的var

[英]javascript how to pass each element in an array to different var

I am new to javascript and recently have a problem to passing elements from array to var. 我是javascript新手,最近在将元素从数组传递到var时遇到问题。

For example, I have an array like var anArray = [[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]], and I have 3 different var a , b and c. 例如,我有一个类似var anArray = [[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]的数组,并且我有3个不同的var a,b和c。

after some loop codes, 在一些循环代码之后,

what I would like to see is: 我想看到的是:

while a=a1, b should be b1 and c=c1 当a = a1时,b应该是b1并且c = c1

while a=a2, b=b2 and c=c2 而a = a2,b = b2和c = c2

while a=a3, b=b3, c=c3 而a = a3,b = b3,c = c3

also pls consider that what if I have array like: 还请考虑一下,如果我有如下数组:

[[a1,a2,a3],[b1,b2,b3]] which will result a=a1 while b=b1, a=a2 while b=b2 etc. [[a1,a2,a3],[b1,b2,b3]]将导致a = a1,而b = b1,a = a2,b = b2等。

or [[a1,a2],[b1,b2],[c1,c2]] which will result a=a1 while b=b1 and c=c1, a=a2 while b=b2 and c=c2 或[[a1,a2],[b1,b2],[c1,c2]]将导致a = a1,而b = b1和c = c1,a = a2,而b = b2和c = c2

If my question is still not clear enough, please comments it and I will update it. 如果我的问题仍然不够清楚,请发表评论,我将对其进行更新。

I really appreciate all the comments and the code that you have made! 我非常感谢您提供的所有注释和代码! Many thanks! 非常感谢!

You have a bunch of pieces backwards and in the wrong place: 您在错误位置放了很多东西:

var anArray = [[1,2],[1,2]]; 
for(var i=0;i <= anArray.length - 1;i++)
{
    for(var j=0;j<anArray[i].length;j++){
        var a = anArray[i][j];
        var b = anArray[i + 1][j];
        alert("a: "+a+" and b: "+b);
    }

}

Edit: adjusted after you changed your entire question. 编辑:更改整个问题后进行调整。

After you clarified your question I got a general idea of what you want. 在您澄清了问题之后,我对您想要的东西有了一个大致的了解。 I think this is what you want to achieve. 我认为这是您想要实现的。 The example is dynamic so that as long as the length of item in the array is equal. 该示例是动态的,因此只要数组中项目的长度相等即可。

  var anArray = [[1,2,3],[4,5,6],[7,8,9]]; for(var j=0;j<anArray[0].length;j++){ var values = []; for(var i=0;i<anArray.length;i++) { values[i] = anArray[i][j]; } //Do what you want with the values below. I chose to show them in a alert message var text = ''; for(var i=0;i<anArray.length;i++) { if(text.length>0){ text += ',' }; text += values[i]; } window.alert('Values: ' + text); } 

PS: There were not 1 but a few things wrong with your code. PS:代码不是1,而是一些错误。

Let say that we have have an array like this var anArray = [[1,2,3],[4,5,6]]; 假设我们有一个像这样的数组var anArray = [[1,2,3],[4,5,6]]; and if you want to alert this a=14;b=25;c=36; 并且如果您想对此进行警告,a = 14; b = 25; c = 36; then you can use this code 那么你可以使用这段代码

var anArray = [[1,2,3],[4,5,6]];
for ( i = 0; i < anArray.length; i++ ) {
    var  l = anArray[i];
    for ( m = 0; m < l.length; m++ ){
        this["a"+i+m.toString()] = l[m];
    }
}
alert("a = "+ a00 + a10.toString());
alert("b = "+ a01 + a11.toString());
alert("c = "+ a02 + a12.toString());

Where a00=1; 其中a00 = 1; a01=2; A01 = 2; a02=3; A02 = 3; these are the elements of the first array.(the middle nr. tells the array so for the first array we use 0). 这些是第一个数组的元素。(中间的nr。告诉数组,因此对于第一个数组,我们使用0)。 Then we have a10=4; 那么我们有a10 = 4; a11=5; A11 = 5; a12=6; A12 = 6; these are the elements of the 2nd array.(the middle nr. tells the array so for the second array we use 1). 这些是第二个数组的元素。(中间的nr。告诉该数组,因此对于第二个数组,我们使用1)。

All you have to do is just replace this array var anArray = [[1,2,3],[4,5,6]] with yours and let javascript do its job. 您所要做的只是用您的数组替换该数组var anArray = [[1,2,3],[4,5,6]] ,然后让javascript完成其工作。

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

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