2015-09-22 126 views
0

如果我的变量命名test_var有此阵:删除特定的字符

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

怎么可能收回这个

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

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

回答

3

使用map()match()

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

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

 
console.log(arr);

3

,你可以使用列表理解:

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

a.map(function(x) { return x.substring(2)}) 

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