循環我有2個陣列對於2個解析陣列
var a = [
"4",
"@",
"/\\",
"/-\\",
"^",
"∂",
"λ",
"α",
"(!",
"Z",
"α"
];
var r = [
"1²",
"2",
"?",
"P\\",
"[\"/_",
"l\"/_",
"|-",
"|2",
"|?",
"®",
"12",
"/2",
"I2",
"|^",
"|~",
"(r)",
"|`",
"l2",
"Я",
"ʁ",
"я"
];
我需要收杆到1聲明兩個陣列
以下是我有:
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);
};
輸出:
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: я
問題是一個數組更長然後是另一個,並且在完成解析最短的一個之後,它繼續最長的一個,但是最短的那個值是未定義的。當最短的數組結束時,我需要保存最後一個值。我怎樣才能做到這一點。 謝謝。
你只需要最後一個元素? – baao
當第一個數組結束時不清楚你想要做什麼 - 停止對第二個數組的循環或僅繼續循環? –
爲什麼不合並數組並迭代所有項目? – Alex