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 badges
Add a ment  | 

3 Answers 3

Reset to default 8

This 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