2016-11-13 73 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类似,但在这种情况下地图就是你想要使用的。

相关问题