这是数据如何给我,在一个对象w /元素由管道char分隔。
首先我需要将两个数组合并然后按字母顺序排序。 以下示例是一个简化示例。 但有些事情正在抛弃排序功能。结果很奇怪
carriersOne = ['St. Joseph\'s Medical Center | New York Health Care Insurance Company | Some Other Company'];
carriersTwo = ['Advantage Care | Chicago Insurance Company | Hospital Insurance Corporation'];
carriersOne = carriersOne[0].split('|');
carriersTwo = carriersTwo[0].split('|');
allCarriers = carriersOne.concat(carriersTwo);
allCarriers.sort();
count = allCarriers.length;
for(i=0;i<count;i++) {
alert(allCarriers[i]);
}
你得到的是:
Chicago Insurance Company
Hospital Insurance Corporation
New York Health Care Insurance Company
Some Other Company
Advantage Care
St. Joseph's Medical Center
w-t-bleep命令是什么?
注意:如果您使用单个名称或可预测的名字和姓氏,它会合并并排序。
答案 0 :(得分:5)
那是因为你没有剥离周围的空间,特别是领先的空间。排序已关闭,因为空格出现在任何字母之前。假设整行没有周围的空白区域,下面的代码应该修复它:
// split on pipe and surrounding white space
var splitRe = /\s*\|\s*/;
carriersOne = carriersOne[0].split(splitRe);
carriersTwo = carriersTwo[0].split(splitRe);
答案 1 :(得分:1)
你需要修剪空格:
carriersOne = carriersOne[0].split('|').map(function(e){return e.replace(/^\s*/, '')});
carriersTwo = carriersTwo[0].split('|').map(function(e){return e.replace(/^\s*/, '')});
答案 2 :(得分:0)
这是管道之间的空间。这是一个更加发达(更灵活)的解决方案。
http://jsfiddle.net/THEtheChad/3v7ss/
var carriersOne = ['St. Joseph\'s Medical Center | New York Health Care Insurance Company | Some Other Company'];
var carriersTwo = ['Advantage Care | Chicago Insurance Company | Hospital Insurance Corporation'];
var allCarriers = parseAndMerge(carriersOne, carriersTwo).sort();
var count = allCarriers.length;
allCarriers.forEach(function(carrier){
alert(carrier);
});
function parseAndMerge(){
var args = Array.prototype.slice.call(arguments)
, collector = []
;//var
args.forEach(function(arr){
collector = collector.concat(arr[0].split('|').map(trim));
});
return collector;
}
function trim(string) { return string.trim() }
管道|
之间的空格导致排序顺序不正确。尝试这样的事情:
http://jsfiddle.net/THEtheChad/LQxW8/
carriersOne = ['St. Joseph\'s Medical Center | New York Health Care Insurance Company | Some Other Company'];
carriersTwo = ['Advantage Care | Chicago Insurance Company | Hospital Insurance Corporation'];
carriersOne = normalize(carriersOne);
carriersTwo = normalize(carriersTwo);
allCarriers = carriersOne.concat(carriersTwo);
allCarriers.sort();
count = allCarriers.length;
for(i=0;i<count;i++) {
alert(allCarriers[i]);
}
function normalize(arr){
var carriers = arr[0].split('|')
, i = carriers.length
;//var
while(i--){
carriers[i] = carriers[i].trim();
}
return carriers;
}
答案 3 :(得分:0)
问题是您在运营商名称中有前导空格。
[" Some Other Company", " Chicago Insurance Company ", " Hospital Insurance Corporation", " New York Health Care Insurance Company ", "Advantage Care ", "St. Joseph's Medical Center "]
您可以移除前导空格并获得您正在寻找的结果:
carriersOne = ['St. Joseph\'s Medical Center | New York Health Care Insurance Company | Some Other Company'];
carriersTwo = ['Advantage Care | Chicago Insurance Company | Hospital Insurance Corporation'];
carriersOne = carriersOne[0].split('|');
carriersTwo = carriersTwo[0].split('|');
allCarriers = carriersOne.concat(carriersTwo);
allCarriers = allCarriers.map(
function(carrier){
// replace leading and trailing spaces
return carrier.replace(/^\s+|\s+$/g,'');
}
);
allCarriers.sort();
console.log(allCarriers);
答案 4 :(得分:0)
空格的ASCII为32,大写字符从65开始,小写字母从97开始。这就是为什么空格在A和a之前排序的原因。
您可能希望使用正则表达式或仅使用underscore.js
来修剪它