admin管理员组文章数量:1302374
I am trying to append the count to duplicate entries in a string array. I have an array like this which contains duplicate entries.
var myarray = ["John", "John", "John", "Doe", "Doe", "Smith",
"John", "Doe", "Joe"];
My desired output is
var newArray = ["John - 1", "John - 2", "John - 3", "Doe - 1",
"Doe - 2", "Smith", "John - 4", "Doe - 3", "Joe"];
What is the best way to do this?
I am trying to append the count to duplicate entries in a string array. I have an array like this which contains duplicate entries.
var myarray = ["John", "John", "John", "Doe", "Doe", "Smith",
"John", "Doe", "Joe"];
My desired output is
var newArray = ["John - 1", "John - 2", "John - 3", "Doe - 1",
"Doe - 2", "Smith", "John - 4", "Doe - 3", "Joe"];
What is the best way to do this?
Share asked Jan 9, 2012 at 19:42 RamRam 512 silver badges6 bronze badges3 Answers
Reset to default 8This works, using two passes of Array.map()
:
var map = {};
var count = myarray.map(function(val) {
return map[val] = (typeof map[val] === "undefined") ? 1 : map[val] + 1;
});
var newArray = myarray.map(function(val, index) {
if (map[val] === 1) {
return val;
} else {
return val + ' - ' + count[index];
}
});
The first pass records the number of times each unique item has been seen, and returns an array corresponding to the input array recording the current count for each item.
The second pass appends the count to any items whose total count was not one.
Working demo at http://jsfiddle/alnitak/Z4dgr/
something like
var counts = {}, newArray = [];
// first pass, assign the counts
for (var i = 0; i < myArray.length; i++) {
var name = myArray[i],
count = counts.name;
if (!count) {
count = 1;
} else {
count++;
}
counts[name] = count;
name = name + " - " + count;
newArray.push(name);
}
// Undo changes made for names that only occur once
for (var i = 0; i < myArray.length; i++) {
var name = myArray[i],
count = counts.name;
if (count === 1) {
newArray[i] = name;
}
}
note I'm using very simple javascript to keep the solution easily readable, understandable. Note you have to pass over the array twice. For very large lists, this is not very efficient.
I didn't test this, but something like that should work.
This should tell you the count of all duplicates:
var myarray = ["John", "John", "John", "Doe", "Doe", "Smith",
"John", "Doe", "Joe"];
var values = {}, newArray = [];
for (var i = 0; i < myarray.length; i++) {
if (typeof values[myarray[i]] === 'undefined') {
values[myarray[i]] = 1;
} else {
values[myarray[i]] += 1;
}
}
for (var i = 0; i < myarray.length; i++) {
if (values[myarray[i]] > 1) {
newArray.push(myarray[i] + ' - ' + values[myarray[i]]);
} else {
newArray.push(myarray[i]);
}
}
Best regards!
本文标签: Appending the count to duplicates in a javascript string arrayStack Overflow
版权声明:本文标题:Appending the count to duplicates in a javascript string array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741712633a2393928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论