开发者

Cleaning variables passed to function using $.each()

I have t开发者_运维问答he following:

setup : function(first, middle, last) {

    // Clean input.
    $.each([first, middle, last], function(index, value) {

        ??? = value.replace(/[\W\s]+/g, '').toLowerCase();
    });

Is there a way I could get that to work? I've tried to figure out what to substitute instead of ??? (I've tried this, index, this[index], but can't seem to wrap my head around pointing to the original variables.

Thanks for any help.


Use the Arguments object.

setup : function(first, middle, last) {
    var args = arguments;
    $.each(arguments, function(index, value) {
        args[index] = value.replace(/[\W\s]+/g, '').toLowerCase();
    });

Example: http://jsfiddle.net/EfHQ2/


To modify arrays, use $.map() instead, and just return the new value:

var clean = $.map([first, middle, last], function(value, index) {
    return value.replace(/[\W\s]+/g, '').toLowerCase();
});

Better, use the special arguments object (as in Patrick's answer) in place of building a temporary array:

var clean = $.map(arguments, function(value, index) {
    return value.replace(/[\W\s]+/g, '').toLowerCase();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜