从数组中删除特定字符

时间:2015-09-22 15:25:15

标签: javascript jquery arrays

如果名为test_var的变量具有此数组:

socket.broadcast.emit('hi');

怎样才能收回这个

[". test.1", ". test/2", ". test.3"]

这只是一个例子我不知道我的数组可以拥有多少个键/值,因为它在动态代码中,并且如果它们都有我要删除的["test.1", "test/2", "test.3"] 字符。是否有任何简单的方法来检查我的数组,如果它包含前一个字符删除,否则保持原样?

2 个答案:

答案 0 :(得分:3)

你可以使用列表理解:

.Destroy(update => update.Action("Process_Destroy", "controller name"))
and in controller,

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Process_Destroy([DataSourceRequest] DataSourceRequest  request, ProductViewModel product)
{
if (product != null)
{
    //write your code for delete action;
}

return Json(ModelState.ToDataSourceResult());
}

对于更复杂的转换,您可能需要使用正则表达式

答案 1 :(得分:3)

使用 map() match() 来执行此操作



var arr = [". test.1", ". test/2", ". test.3"];

arr = arr.map(function( v) {
  return v.match(/\b.*$/)[0];
});

console.log(arr);