0

I am trying to get the unique data out of the rows.

My data

name  title
tim    1
tim    1
tim    2
tim    3
time   3
jenny  5
jenny  5
jenny  6
jenny  7

My goal is to display my table using javascript to show

tim    1
tim    2
time   3
jenny  5
jenny  6
jenny  7

I know it's easy to change the data structure or the way to select the data but I don't have the access to the DB so I need to figure out in the javascript

my first step is to push all the unique title to my peopleArray:

1
2
3
5
6
7

The codes I have tried.

 for(var a=0; a<results.length; a++){
        if(results[(a+1)]){
             if(a==0 || results[a].title != results[(a+1)].title){

                peopleArray.push(results[(a)].title)
             }
         }
      }

My codes don't really work and I am stocked here. Anyone has tips for this? Thanks a lot!

5
  • 1
    create a composite key using a delimiter and push into array and then check array.indexOf(compositeKey) to find if its already pushed. once making a pass through all the rows, pop all the entries in the array and remove the delimiter. Commented Dec 13, 2012 at 19:37
  • What is the literal data structure type of your data? Can you post it as json? Commented Dec 13, 2012 at 19:38
  • @user711819 javascript doesn't have a native contains. Need to use Array.indexOf(), but if it's an array of dictionaries (objects) then you can't use indexOf either. Would have to do a map first. Commented Dec 13, 2012 at 19:40
  • agreed, indexOf(). even if its an array of dicts/objects you can still loop over the objects and construct the compositekeys that can help you to identify unique objects. Commented Dec 13, 2012 at 19:43
  • @user711819 Or you could just JSON stringify the objects to get a uniform composite. Commented Dec 13, 2012 at 20:00

3 Answers 3

1

For something that does not require the list to be sorted, you can use something like this:

http://jsfiddle.net/4dvfq/1/

var results = [{"name": "tim", "title": "1"},
               {"name": "jenny", "title": "2"},
               {"name": "tim", "title": "1"},
               {"name": "jenny", "title": "1"}];
// Should return "tim 1", "jenny 2", "jenny 1" items

function getUniques(arr) {
    var i = 0;
    var len = arr.length;
    var found = {};
    var ret = [];

    for (; i < len; i++) {
        var cur = arr[i];
        var name = cur.name;
        var title = cur.title;
        var name_title = name + title;

        if (!(name_title in found)) {
            ret.push(cur);
            found[name_title] = 1;
        }
    }
    return ret;
}

console.log(getUniques(results));
Sign up to request clarification or add additional context in comments.

Comments

1

If it's already sorted like you have in your question then you can use Array.filter():

data = data.filter(function (d, i) {
    var next = data[i+1] || {name: '', title: ''};
    return d.name != next.name || d.title != next.title;
});

If it's not sorted yet as you have in your question, do this first:

data = data.sort(function (a, b) {
    return a.name < b.name ? -1 : a.name == b.name ? 0 : 1;
});

2 Comments

I think you want ||, not && in the return statement for the filter method
No problem. I tested it out and it was acting weird :)
0
function uniquify(results) {    
    var peopleArray = [];
    for (var a = 0; a < results.length; a++) {
        if (peopleArray.indexOf(JSON.stringify(results[a])) == -1) {
            peopleArray.push(JSON.stringify(results[a]));
        }
    }
    return peopleArray.map(function(val) {
        return JSON.parse(val);
    });
}

This works regardless of sort order and is flexible enough to be used with other schema. Here is a demonstration that uses your data: http://jsfiddle.net/w4cKg/

3 Comments

I don't think you can do .indexOf with an object because you cannot compare objects.
@BrianCray I suspected as much, I was editing my answer. It compares strings now.
Good approach, but stringifying everything can be a huge performance cost

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.