有人可以帮我用JavaScript执行以下操作:
谢谢。
答案 0 :(得分:2)
//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);
答案 1 :(得分:0)
使用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。