2016-11-13 66 views
-2

我試圖讓「字」的列表我們說的JavaScript添加單詞表到每個環節

var words=["aa","ab","ac","ad","ae"]; 

,然後打印出每個詞的鏈接。 假設我的鏈接是test.com/,我想要做的是將每個單詞添加到該鏈接,但是要單獨添加。因此,這將打印出:http://test.com/aa , http://test.com/ab , http://test.com/ac , http://test.com/ad, http://test.com/ae

+0

你提供的代碼是JavaScript?在PHP中,你可以使用一個foreach循環。 http://php.net/manual/en/control-structures.foreach.php – 2016-11-13 21:45:27

+1

很酷。祝你好運!你有問題嗎? –

+0

@Hallur是的,它的JavaScript。我要去嘗試使用foreach循環感謝:) – Devon

回答

0

通過使用陣列上的地圖構造器,您可以修改每個項目

words.map(function(val){ 
    return "http://test.com/" + val //Add http://test.com/ before each value in the array 
}) 

給你的

["http://test.com/aa", "http://test.com/ab", "http://test.com/ac", "http://test.com/ad", "http://test.com/ae"] 

數組那麼,什麼地圖所做的就是獲取數組中的每個值,運行一個函數並返回一個值。這個新值將替換使用的數組部分。它與forEach類似,但在這種情況下地圖就是你想要使用的。

相關問題