Javascript map method on array of string elements

后端 未结 8 455
悲哀的现实
悲哀的现实 2021-01-11 10:09

I am trying to understand how to implement the map method (rather than using a for loop) to check a string for palindromes and return boolean values for whether the mapped a

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 11:07

    Map is a higher-order function available in ES5. I think your newArraywill contain an array of boolean values.

    In essence, map will iterate over every value in your array and apply the function. The return value will be the new value in the array. You can also use map and save the information you need somewhere else, and ignore the result of course.

    var arr = [1,2,3,4];
    var newArray = arr.map(function(i) {
      return i * 2;
    });
    //newArray = [2,4,6,8]
    

提交回复
热议问题