3

I have an array which contains the following items:

var jobName = getNextJobName();
var Details = getNextJobDetails();
var item = {
          Name: jobName,
          Details: Details
        };
array.push(item);

I want to extract all the unique names (from the Name field). (in order to create a new object which groups all the details for a specific job).

Could you please show me an efficient way to do this?

3
  • 'unique names' means what? you need an array or you want individual variables. Commented Oct 4, 2012 at 9:06
  • Could you show the structure of the object, or the array containing the object(s), are there multiple objects to be compared? As it stands you've only got one name, which is unique by definition. Commented Oct 4, 2012 at 9:07
  • Thanks for your comments, I need an array of all the unique job names. I've edited the code, please tell me if it is clear now. Commented Oct 4, 2012 at 9:11

3 Answers 3

5

You mean have a list of those objects, and you want to extract all the unique values?

Here's a pretty efficient way, storing the values as keys of a new object called uniq and the value being an array of the 'Details' element.

var arr = [ { Name: 'Alex', Details: 'Foo' }, { Name: 'Igor', Details: 'None' }, { Name: 'Alex', Details: 'Boo' } ];
var uniq = {}; // contains all the unique names
for(var i=0; i<arr.length; i++) {
  var el = arr[i].Name;
  if(!uniq[el]) uniq[el] = []; // start the array
  uniq[el].push(arr[i].Details);
}

http://jsfiddle.net/lmatteis/8LWMH/4/

Sign up to request clarification or add additional context in comments.

3 Comments

Then after that all you have to do is to iterate over your and check in the uniq object if the key "Name" has a value greater than 1. But be aware: This solution will not work if your names contain characters that are no valid for JSON keys.
Right yeah. But his question changed. He wants to group all the 'Details' stuff.
Oh, I did not see that. I always love when that happens, specially if all of a sudden all given answers are wrong by miles! Good solution, I'd accept it!
2

You can use an object to do the matching efficiently, then read the names from the object into an array:

var unique = {};
for (var i = 0; i < jobs.length; i++) {
    unique[jobs[i].Name] = 1;
}
var uniqueArray = [];
for (n in unique) uniqueArray.push(n);

Demo: http://jsfiddle.net/Guffa/4cH4F/

2 Comments

This solution only removes things that have duplicate names, in the resulting array they are unique, but they were not unique before.
@clentfort: No, this gets an array of all the unique job names, exactly what the OP asked for.
1

The following code snippet should do the trick:

var _bzAppUnique = 0; //unique variable to increase at each call
function uniqueID() {
    var d = new Date();
    var u = d.getFullYear() + "," + d.getMonth() + "," + 
            d.getHours() + "," + d.getMinutes() + "," + 
            d.getSeconds() + "," + _bzAppUnique;
    _bzAppUnique++;
    return u.replace(/\,/g, "");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.