2017-02-14 86 views
1

只是想知道是否可以完成這樣的事情以及如何完成。我的目標是從一個數組中取出所有數字,並將它們與另一個數組的索引進行比較,最後打印這些元素。將一個數組中的數字與另一個數組的索引進行比較Javascript

實施例:

var myindex = new Array(2, 0); 

var array1 = new Array("text", "apple", "sample") 
var array2 = new array("book", "phone", "orange") 

myindex.forEach(number => document.write(array1.indexOf(number))) 

預期結果:以下將被打印:樣品橙色文本書

第一個問題:所有被示出的是-1錯誤,這意味着我認爲:在沒有數數組,但有一個,所以我不明白? 第二個問題:我還需要實現array2與array1一起工作,以便我們可以實際看到如上所示的預期結果。我希望得到任何幫助

回答

0

你可以簡單地通過索引數組循環,然後使用它的值:

var myIndex = [2,0]; 
 
var array1 = new Array("text", "apple", "sample"); 
 
var array2 = new Array("book", "phone", "orange"); 
 

 
for (var i = 0; i < myIndex.length; i++){ 
 
    console.log(array1[myIndex[i]] + " " + array2[myIndex[i]]); 
 
}

+0

出於某種原因,它不會在我的.html和該網站上的工作,以及:http://codepen.io/anon/pen/vgPJWM?editors=0010 // ///這正是我想要做的,但它根本不顯示,就像你的代碼一樣,看不到任何錯誤 – Jay

0

你可以使用一個陣列中的數據,並檢查是否在數據給定的索引存在。然後製作輸出。

var myindex = [2, 0], 
 
    array = [["text", "apple", "sample"], ["book", "phone", "orange"]]; 
 

 
myindex.forEach(number => array[number] && console.log(array[number]));

+0

你知道爲什麼它在這裏不工作嗎? http://codepen.io/anon/pen/ZLPJxX?editors=0010 ///只能在這裏的'snippet'編輯器中使用。 – Jay

+0

我並沒有太多地使用控制檯,但是如果你用'document.write'替換'console.log',你會得到想要的結果。 –

0

你不需要indexOf這裏,它搜索的數組。你需要通過指數(方括號)來訪問數組來獲取值:

var myindex = new Array(2, 0); 
 

 
var array1 = new Array("text", "apple", "sample") 
 
var array2 = new Array("book", "phone", "orange") 
 

 
myindex.forEach(number => console.log(array1[number], array2[number]));

NB:你曾在數組構造一個錯字:它需要Array() ,而不是array()。其次,不要使用document.write

+0

感謝,真正有用的解釋,訪問索引 – Jay

+0

注:一旦你有15點的聲譽(容易得到),你可以[投票答案](http://stackoverflow.com/help/privileges/vote-up)。另外,不要忘記[將一個答案標記爲已接受](http://stackoverflow.com/help/someone-answers)。 – trincot

0

您只需要使用array[index]語法訪問數組元素。

var myIndexs = [2, 0]; 
 

 
var array1 = ["text", "apple", "sample", "book"]; 
 
var array2 = ["book", "phone", "orange", "text"]; 
 

 
myIndexs.forEach((index) => document.write(`${array1[index]} ${array2[index]} `));

另外,使用[]語法來創建數組,而不是Array()https://coderwall.com/p/h4xm0w/why-never-use-new-array-in-javascript

如果你想在兩行的結果,以及它種取決於你的使用情況,您只是真的想要寫入HTML,你可以簡單地這樣做:

var myIndexs = [2, 0]; 
 

 
var array1 = ["text", "apple", "sample", "book"]; 
 
var array2 = ["book", "phone", "orange", "text"]; 
 

 
const results1 = []; 
 
const results2 = []; 
 

 
myIndexs.forEach((index) => { 
 
    // Here you might want to validate if array1[index] exists 
 
    results1.push(array1[index]); 
 

 
    // Here you might want to validate if array2[index] exists 
 
    results2.push(array2[index]); 
 
}); 
 

 
document.write(`${results1}<br />${results2}`);

但是,它可能取決於您的使用情況。我可能希望在html中創建兩個不同的節點目標,並將每個結果寫入不同的目標。您可能只需要javascript中的兩個數組變量。你不應該盲目地遵循任何代碼,你應該適應你的具體用例。

+0

局部斷線如何?我正在尋找這樣的東西: (array 1 first row)= sample [2] text [0] (array 2 second row)= orange [2] book [0] – Jay

+0

這就像一個魅力,東西是我試圖修改現有的代碼在一行內,我不明白爲什麼它不會工作。例如。像這樣:\t //myIndexs.forEach((index)=> document.write('$ {array1 [index]}
$ {array2 [index]}'));由於這不起作用,我試圖操縱你更新的並且實現不同的方法,因爲我不能讓自己創建更多的數組,但不幸的是沒有運氣。只是想知道有沒有辦法? – Jay

+0

例如這將工作,但有三條線,我需要他們成爲一個完整的行(相應地調整,不僅與空間),我不知道該怎麼做:1. \t myIndexs.forEach((index)=> document .write('$ {array1 [index]}')); 2. \t document.write(「
」); 3. \t myIndexs.forEach((index)=> document.write('$ {array2 [index]}')); – Jay

0

只是爲了一些變化和樂趣,你可能會這樣做;

var myIndex = new Array(2, 0), 
 
    array1 = new Array("text", "apple", "sample"), 
 
    array2 = new Array("book", "phone", "orange"); 
 
console.log.apply(console, myIndex.reduce((p,c) => p.concat(array1[c],array2[c]),[]));

相關問題