如何从字符串数组中删除逗号
How do I remove commas from an array of strings
我有一个数组列表:
0: "1 Trenchard Road, , , , Saltford, Bristol, Avon"
1: "10 Trenchard Road, , , , Saltford, Bristol, Avon"
2: "11 Trenchard Road, , , , Saltford, Bristol, Avon"
3: "12 Trenchard Road, , , , Saltford, Bristol, Avon"
我想去掉中间的逗号:
, , ,
我正在使用 Lodash 并查看了使用 _.compact()
但这似乎并没有让我到任何地方。
让我知道你的想法
更新
var addressArray = getAddressData.data.Addresses;
scope.items = _.compact(addressArray);
console.log(scope.items);
您可以通过逗号拆分,filter 空白并合并。
var str = "1 Trenchard Road, , , , Saltford, Bristol, Avon";
var result = str.split(',').filter(x => x.trim()).join()
console.log(result); // 1 Trenchard Road, Saltford, Bristol, Avon
注意:使用 ES6 arrow function (=>
) 如果它在您的环境中不起作用,您可以将其替换为经典 function
。
具有 map 函数的完整示例:
let arr = [
'1 Trenchard Road, , , , Saltford, Bristol, Avon',
'10 Trenchard Road, , , , Saltford, Bristol, Avon',
'11 Trenchard Road, , , , Saltford, Bristol, Avon',
'12 Trenchard Road, , , , Saltford, Bristol, Avon'
];
let result = arr.map(i => i.split(',').filter(x => x.trim()).join());
ES5 等价物:
var result = arr.map(function(i) {
return i.split(',').filter(function(x) {
return x.trim();
}).join();
});
您可以使用 JavaScript Array#map
和 RegEx 来删除多余的逗号。
arr.map(e => e.replace(/(,\s*)+/, ','));
等效于 ES5:
arr.map(function (e) {
return e.replace(/(,\s*)+/, ',');
});
正则表达式 (,\s*)+
将搜索一个或多个由任意数量的空格分隔的逗号。
var arr = ["1 Trenchard Road, , , , Saltford, Bristol, Avon", "10 Trenchard Road, , , , Saltford, Bristol, Avon", "11 Trenchard Road, , , , Saltford, Bristol, Avon", "12 Trenchard Road, , , , Saltford, Bristol, Avon"];
arr = arr.map(e => e.replace(/(,\s*)+/, ', '));
console.log(arr);
document.getElementById('result').innerHTML = JSON.stringify(arr, 0, 4);
<pre id="result"></pre>
使用数组过滤功能
["1 Trenchard Road", , , , "Saltford", "Bristol", "Avon"].filter(function(i){return i})
请在下面找到JS中的解决方案:
for(var i=0;i<arr.length;i++) {
if(arr[i].indexOf(',') > -1) {
console.log(arr[i].replace(/,/g,''));
}
}
我有一个数组列表:
0: "1 Trenchard Road, , , , Saltford, Bristol, Avon"
1: "10 Trenchard Road, , , , Saltford, Bristol, Avon"
2: "11 Trenchard Road, , , , Saltford, Bristol, Avon"
3: "12 Trenchard Road, , , , Saltford, Bristol, Avon"
我想去掉中间的逗号:
, , ,
我正在使用 Lodash 并查看了使用 _.compact()
但这似乎并没有让我到任何地方。
让我知道你的想法
更新
var addressArray = getAddressData.data.Addresses;
scope.items = _.compact(addressArray);
console.log(scope.items);
您可以通过逗号拆分,filter 空白并合并。
var str = "1 Trenchard Road, , , , Saltford, Bristol, Avon";
var result = str.split(',').filter(x => x.trim()).join()
console.log(result); // 1 Trenchard Road, Saltford, Bristol, Avon
注意:使用 ES6 arrow function (=>
) 如果它在您的环境中不起作用,您可以将其替换为经典 function
。
具有 map 函数的完整示例:
let arr = [
'1 Trenchard Road, , , , Saltford, Bristol, Avon',
'10 Trenchard Road, , , , Saltford, Bristol, Avon',
'11 Trenchard Road, , , , Saltford, Bristol, Avon',
'12 Trenchard Road, , , , Saltford, Bristol, Avon'
];
let result = arr.map(i => i.split(',').filter(x => x.trim()).join());
ES5 等价物:
var result = arr.map(function(i) {
return i.split(',').filter(function(x) {
return x.trim();
}).join();
});
您可以使用 JavaScript Array#map
和 RegEx 来删除多余的逗号。
arr.map(e => e.replace(/(,\s*)+/, ','));
等效于 ES5:
arr.map(function (e) {
return e.replace(/(,\s*)+/, ',');
});
正则表达式 (,\s*)+
将搜索一个或多个由任意数量的空格分隔的逗号。
var arr = ["1 Trenchard Road, , , , Saltford, Bristol, Avon", "10 Trenchard Road, , , , Saltford, Bristol, Avon", "11 Trenchard Road, , , , Saltford, Bristol, Avon", "12 Trenchard Road, , , , Saltford, Bristol, Avon"];
arr = arr.map(e => e.replace(/(,\s*)+/, ', '));
console.log(arr);
document.getElementById('result').innerHTML = JSON.stringify(arr, 0, 4);
<pre id="result"></pre>
使用数组过滤功能
["1 Trenchard Road", , , , "Saltford", "Bristol", "Avon"].filter(function(i){return i})
请在下面找到JS中的解决方案:
for(var i=0;i<arr.length;i++) {
if(arr[i].indexOf(',') > -1) {
console.log(arr[i].replace(/,/g,''));
}
}