how to map an array with uppercase function in javascript?

前端 未结 10 1268
野趣味
野趣味 2021-02-12 19:29

I\'m interested if there is any function like array_map or array_walk from php.

Don\'t need an for that travels all the array. I can do that for myself.



        
相关标签:
10条回答
  • 2021-02-12 20:07

    This may be an overkill but you can also do

    var upperCaseArray = array.map(String.prototype.toUpperCase.call.bind(String.prototype.toUpperCase));
    
    0 讨论(0)
  • 2021-02-12 20:10

    Similar to a previous for loop answer, this one uses some of the more basic features of javascript:

    function looptoUpper(arr){
        var newArr = [];
        for (var i = 0; i < arr.length; i++){
            newArr.push(arr[i].toUpperCase());
        }
        return newArr;
    }
    looptoUpper(['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab']);
    
    0 讨论(0)
  • 2021-02-12 20:15

    Javascript has a map() method. A good reference is at http://www.tutorialspoint.com/javascript/array_map.htm

    0 讨论(0)
  • 2021-02-12 20:23

    And here is the one-liner without creating a loop or a function that is ES3 compatible It's alos faster since you only call toUpperCase once

    // watch out for strings with comma (eg: ["hi, max"]) then use custom join/split()
    // or just use something safer such as Array.map()
    var uppercaseArray = array.toString().toUpperCase().split(",");
    var uppercaseArray = array.join(0).toUpperCase().split(0);
    var uppercaseArray = (array+"").toUpperCase().split(",");
    
    0 讨论(0)
提交回复
热议问题