我試圖在JavaScript中將整數字符串拆分成一個數組。將整數字符串拆分爲數組 - 元素不被識別
我原本:
m.rows[7] = new Array (8,11);
而且我將其更改爲:
var nearby = nearby.split(",");
m.rows[7] = new Array (nearby);
(並設置有一個逗號在我的關卡編輯器分離合適的整數變量當我打印當我打印「附近的[1]」到控制檯時,我得到11)
但是,我有這個代碼,我試圖匹配其中一個th中的元素È陣列與另一個數組中的元素:
for (var i = m.rows[id].length-1; i >= 0; i--) {
// If the loop finds an element ID that matches the ID of the last element in the path array, do this:
console.log('test nearby 2: ' + m.rows[id][i]);
if (m.rows[id][i] == this.path_array[this.path_array.length-1].id) {
// Loop through the one array
for (var j = all_nodes.length-1; j >= 0; j--){
// If the ID of one of one of these entities matches the id of the instance that was just clicked
if (all_nodes[j].id == id) {
// Activate that node:
all_nodes[j].active = true;
}
}
break;
}
當我居然把「8,11」到上述手動上述工程完美陣列。但是,當我嘗試使用「分離」到數組中的「附近」時,它不會。並在上面的「測試附近2」打印到控制檯,當我使用「附近」時,「8,11」被打印。當我手動輸入「8,11」時,我得到「11」。
我相當新的JavaScript,所以我可能會錯過這裏非常明顯的東西 - 任何人都可以擺脫一些光?
謝謝!
目前還不清楚「附近」究竟是什麼;它是一個逗號分隔值的字符串?如果是這樣,請注意分割它將導致字符串,而不是實際的「數字」。這*可能會沒問題,因爲JS會進行類型轉換,但它取決於操作的順序。更容易/更安全,只需在分割後讓他們編號。 – 2012-03-05 18:54:58
Dave,Nearby是一個用逗號分隔的字符串,如你所建議的。 Minitech的回答下面最終幫助我做我正在嘗試:) – spectralbat 2012-03-05 19:23:27