javascript - How to set sequence for exact consecutive elements of csv string? -
i have string
csv
containing portcode , latitude longitude of location. use these values plot marker on google map
.
eg csv string:
anc|61.2181:149.9003, anc|61.2181:149.9003, tlk|62.3209:150.1066, dnl|63.1148:151.1926, dnl|63.1148:151.1926, dnl|63.1148:151.1926, tlk|62.3209:150.1066, tlk|62.3209:150.1066, ale|60.9543:149.1599
i want autonumber similar portcode sequence separated pipe symbol '|' portcode exact consecutive next element.
required out put:
anc|61.2181:149.9003:1|2, tlk|62.3209:150.1066:3, dnl|63.1148:151.1926:4|5|6, tlk|62.3209:150.1066:7|8, ale|60.9543:149.1599:9
any solution using jquery/javascript/c#
?
there's neater/shorter way this, here's first second way came mind using javascript:
var input = "anc|61.2181:149.9003,\nanc|61.2181:149.9003,\ntlk|62.3209:150.1066,\ndnl|63.1148:151.1926,\ndnl|63.1148:151.1926,\ndnl|63.1148:151.1926,\ntlk|62.3209:150.1066,\ntlk|62.3209:150.1066,\nale|60.9543:149.1599"; var output = input.split(",\n").reduce(function(p,c,i,a) { if (i === 1) p += ":1"; return p + (c === a[i-1] ? "|" : ",\n" + c + ":") + (i+1); }); console.log(output);
i've assumed each line ends single \n
character, can adjust \r
or whatever.
further reading:
Comments
Post a Comment