Replace string in javascript array

后端 未结 7 2156
萌比男神i
萌比男神i 2020-12-08 14:10

I have an array in javascript. This array has strings that contains commas (\",\"). I want all commas to be removed from this array. Can this be done?

相关标签:
7条回答
  • 2020-12-08 15:07

    The best way nowadays is to use the map() function in this way:

    var resultArr = arr.map(function(x){return x.replace(/,/g, '');});
    

    this is ECMA-262 standard. If you nee it for earlier version you can add this piece of code in your project:

    if (!Array.prototype.map)
    {
        Array.prototype.map = function(fun /*, thisp*/)
        {
            var len = this.length;
            if (typeof fun != "function")
              throw new TypeError();
    
            var res = new Array(len);
            var thisp = arguments[1];
            for (var i = 0; i < len; i++)
            {
                if (i in this)
                    res[i] = fun.call(thisp, this[i], i, this);
            }
    
            return res;
        };
    }
    
    0 讨论(0)
提交回复
热议问题