如何映射在JavaScript中大写功能的阵列? [英] how to map an array with uppercase function in javascript?

查看:111
本文介绍了如何映射在JavaScript中大写功能的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣,如果有喜欢array_map任何功能或从PHP array_walk。

不需要一个用于行进的所有的阵列。我能做到这一点我自己。

  VAR阵列= ['DOM,伦,月,聚,新闻局,法师,SAB'];
//如果存在,我想是这样的 - 函数(数组,'大写');


解决方案

您可以使用 $ .MAP()为了适用<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/toUpperCase\">String.toUpperCase() (或<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase\">String.toLocaleUpperCase(),如果合适的话),以您的数组项:

  VAR upperCasedArray = $ .MAP(数组,String.toUpperCase);

注意 $。图()建立一个新的数组。如果你想修改就地现有的阵列,可以使用 $。每个()与匿名函数的:

  $。每个(数组,函数(指数,项目){
    数组[索引] = item.toUpperCase();
});

更新:作为afanasy理所当然地在下面的评论中指出,测绘 String.toUpperCase 直接将仅在基于Gecko的浏览器工作

要支持其他浏览器,你可以提供自己的功能:

  VAR upperCasedArray = $ .MAP(数组,函数(项目,指数){
    返回item.toUpperCase();
});

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.

var array = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
// i would like something like this if exists - function(array, 'upperCase');

解决方案

You can use $.map() in order to apply String.toUpperCase() (or String.toLocaleUpperCase(), if appropriate) to your array items:

var upperCasedArray = $.map(array, String.toUpperCase);

Note that $.map() builds a new array. If you want to modify your existing array in-place, you can use $.each() with an anonymous function:

$.each(array, function(index, item) {
    array[index] = item.toUpperCase();
});

Update: As afanasy rightfully points out in the comments below, mapping String.toUpperCase directly will only work in Gecko-based browsers.

To support the other browsers, you can provide your own function:

var upperCasedArray = $.map(array, function(item, index) {
    return item.toUpperCase();
});

这篇关于如何映射在JavaScript中大写功能的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆