zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Iterate over Arrays with map #279

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

介绍

map可以遍历数组。

方法

var oldArray = [1, 2, 3];
var timesFour = oldArray.map(function(val){
  return val * 4;
});
console.log(timesFour); // returns [4, 8, 12]
console.log(oldArray);  // returns [1, 2, 3]

map方法可以遍历数组的每个元素,创造一个新数组的值,但不会改变原数组。

练习

Use the map function to add 3 to every value in the variable oldArray, and save the results into variable newArray. oldArray should not change.

代码


var oldArray = [1,2,3,4,5];

// Only change code below this line.

var newArray = oldArray.map(function(val){return val+3;});

结果显示

image

来源

https://www.freecodecamp.org/challenges/iterate-over-arrays-with-map