0

I got for example a multidimensional array items with 2 dimensions. I get this array from a database, but it will fill up to 2600+ objects, but if i could some how unique this it would be around 30 objects. How to solve this?

The set up is: How i get the information:

$.getJSON(url1,function(data1)
{
    for (var i in data1.layers){
        $.each(data1.layers[i].legend, function( a, b ) {
            for(var a in data1.layers[i].legend[a]){
                $.each(data1.layers[i].legend, function( key, val ){
                    items.push({label: val.label, url: "long link" + val.url});
                });
            };
        });     
    };

items[0].label  
items[0].url 
items[1].label    
items[1].url

etc...

I found another stackoverflow page about this in php, but i can't get it to work in JavaScript/JQuery. Stackoverflow php solution

Community
  • 1
  • 1
Mike-N88
  • 35
  • 1
  • 5
  • That looks like an array of objects, actually. You need to show where the data is coming from and how you're building the array. – JJJ Dec 03 '13 at 15:29
  • Which parts will be unique? are all 2600 labels and urls similar, or is there just 30 different urls with lots of labels? – vogomatix Dec 03 '13 at 15:33
  • @ Juhana i added the information of the items arrays. @vogomatix It will get ~30 different labels and urls, but it will "clone" them in the array. – Mike-N88 Dec 03 '13 at 15:33

3 Answers3

0

You're making a mistake in pushing them onto an array in the first place. You should be creating an associative array, with the key field the unique aspect.

vogomatix
  • 4,856
  • 2
  • 23
  • 46
0

Use a dictionary to see which item you have already:

var dict = {};

$.getJSON(url1,addToLocalArray);

function addToLocalArray(data1){

  for (var i in data1.layers){

    $.each(data1.layers[i].legend, function( a, b ) {

        for(var a in data1.layers[i].legend[a]){

            $.each(data1.layers[i].legend, function( key, val ){

              if(!dict[val.url+'-'+val.label]){
                // or create some sort of unique hash for each element...
                dict[val.url+'-'+val.label] = 1;

                items.push({label: val.label, url: "long link" + val.url});
              }

            });

        }
    });
  }
}
MarcoL
  • 9,829
  • 3
  • 37
  • 50
  • This does what i wanted thanks, tho i have to admit if i could change things at the server side i would do it. – Mike-N88 Dec 03 '13 at 15:48
0

I would suggest either:

A)Push the Uniqueness filtering logic off to the database. It's much more efficient at this, and won't require exhaustive iteration.

B)Have a key for each unique Item (As vogomatix suggested).

Fydo
  • 1,396
  • 16
  • 29