可能有人請幫我做的JavaScript如下:JS - 從另一個數組填充數組+修改值
- 我有一個數組(數組1)包含字符串
- 我通過每一個需要循環然後我需要得到array1中每個索引的值,然後給每個值添加一個字母
- ,最後將每個索引的修改值從array1寫入到array2中。
謝謝。
可能有人請幫我做的JavaScript如下:JS - 從另一個數組填充數組+修改值
謝謝。
//declares the array and initializes it with strings
var array1 = ["one", "two", "three", "four"];
//declares the second array
var array2 = [];
//this line begins the loop. Everything inside the { } will run once for every single item inside array1
for (var i = 0; i < array1.length; i++)
{
//this gets the contents of the array at each interval
var string = array1[i];
//here we take the original string from the array1 and add a letter to it.
var combo = string + "A";
//this line takes the new string and puts it into the 2nd array
array2.push(combo);
}
//displays a message box that shows the contents of the 2nd array
alert(array2);
非常感謝你。 – MousseDeHumus 2013-02-09 15:59:44
使用Array.map的示例。
var singularArray = ['Dog', 'Cat', 'Bird'],
pluralArray = singularArray.map(function(value){
return value+'s';
});
console.log(pluralArray); //Logs ['Dogs', 'Cats', 'Birds']
詳見https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map。
[你有什麼嘗試?](http://whathaveyoutried.com/) – ruakh 2013-02-08 23:58:08
Array.map是一個好開始 – rlemon 2013-02-09 00:08:23