简体   繁体   English

带2个解析数组的For循环

[英]For loop with 2 parsing arrays

I have 2 arrays 我有2个数组

var a = [
    "4",
    "@",
    "/\\",
    "/-\\",
    "^",
    "∂",
    "λ",
    "α",
    "(!",
    "Z",
    "α"
];
var r = [
    "1²",
    "2",
    "?",
    "P\\",
    "[\"/_",
    "l\"/_",
    "|-",
    "|2",
    "|?",
    "®",
    "12",
    "/2",
    "I2",
    "|^",
    "|~",
    "(r)",
    "|`",
    "l2",
    "Я",
    "ʁ",
    "я"
];

I need to pars into 1 for statement both arrays 我需要将两个数组的语句解析为1

Here is what I have: 这是我所拥有的:

for (var index_a=0, index_r=0; index_a < a.length, index_r < r.length ; ++index_a, ++index_r ) {
    new_a = a[index_a];
    console.log(new_a);
    new_r = r[index_r];
    console.log(new_r);
};

Output: 输出:

the element from A array: 4
the element from R array: 1²
the element from A array: @
the element from R array: 2
the element from A array: /\
the element from R array: ?
the element from A array: /-\
the element from R array: P\
the element from A array: ^
the element from R array: ["/_
the element from A array: ∂
the element from R array: l"/_
the element from A array: λ
the element from R array: |-
the element from A array: α
the element from R array: |2
the element from A array: (!
the element from R array: |?
the element from A array: Z
the element from R array: ®
the element from A array: α
the element from R array: 12
the element from A array: undefined
the element from R array: /2
the element from A array: undefined
the element from R array: I2
the element from A array: undefined
the element from R array: |^
the element from A array: undefined
the element from R array: |~
the element from A array: undefined
the element from R array: (r)
the element from A array: undefined
the element from R array: |`
the element from A array: undefined
the element from R array: l2
the element from A array: undefined
the element from R array: Я
the element from A array: undefined
the element from R array: ʁ
the element from A array: undefined
the element from R array: я

The problem is that one array is longer then the another one, and after finishing parsing the shortest one it continues to the longest one but the value for the shortest one is undefined. 问题在于,一个数组比另一个数组更长,并且在解析完最短的一个数组之后,它继续到最长的一个数组,但是最短的一个数组的值未定义。 I need that when the shortest array ends, the last value to be saved. 我需要最短数组结束时要保存的最后一个值。 How can I do this. 我怎样才能做到这一点。 Thank you. 谢谢。

Check the value that you trying to reference from the short array. 检查您尝试从短数组中引用的值。 If it's undefined take the value from the last item in the short array. 如果未定义,则从短数组的最后一项中获取值。 The ?: operator can check for this ?:操作员可以检查一下

Something like this: new_a = a[index_a]=='undefined'?a[a.length-1]:a[index_a]; 像这样:new_a = a [index_a] =='undefined'?a [a.length-1]:a [index_a];

I'm sure there can be a 1001 actually quite interesting answers on how to do this, but given your setup that you've shown above, the quickest way would simply be to surround: 我敢肯定,关于如何执行此操作实际上可以得到1001个非常有趣的答案,但是鉴于上面已显示的设置,最快的方法就是包围起来:

if(index_a < a.length){
  new_a = a[index_a];
  console.log(new_a);
}

Now if you have 5 arrays you need to do this with, it's going to get cumbersome to have to brute force each one with a statement. 现在,如果您有5个数组需要执行此操作,则必须用一条语句强行强制每个数组,这将变得很麻烦。 A more fun way would be to start thinking in terms of another method that returns back the nth element of an array if it exists, and nothing if it doesn't. 一种更有趣的方法是开始考虑另一种方法,该方法将返回数组的第n个元素(如果存在),否则不返回任何内容。 That way you can ignore the return value if it is nothing, but use it if it exists. 这样,如果返回值不为空,则可以忽略该返回值,但如果存在则使用它。

If all the arrays in question return back nothing, then you're done. 如果所有有问题的数组什么都不返回,那么您就完成了。

A little simplistic, since there will be some edge cases that can be interesting to have to consider, but this might be a nice way to get going and learn more about those edge cases, or even exception throwing and catching. 有点简单,因为将要考虑一些边缘情况,但是这可能是一个不错的方法,可以让您进一步了解这些边缘情况,甚至可以抛出和捕获异常。

Lots of neat ways to get this done. 许多精巧的方法可以完成此任务。

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

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