And*_* SK 2 javascript arrays string
我有这个javascript var:
var mylist = '1,5,3,7,9,8,44,6';
Run Code Online (Sandbox Code Playgroud)
我需要删除9,因此最终值为:1,5,3,7,8,44,6
我通常用php(简单方法)做这个服务器端.如何用javascript实现这一目标?使用jQuery的解决方案会更好.
注意事项:如果var为'99,9,96'或'9'或'99,9'或'9,98'等,它也应该起作用.
my_list = my_list.split(',').filter(function(e) { return e != 9}).join(',');
稍后编辑:在IE中支持array.filter:
//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}