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

3
  • 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. Commented Dec 3, 2013 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? Commented Dec 3, 2013 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. Commented Dec 3, 2013 at 15:33

3 Answers 3

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});
              }

            });

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

1 Comment

This does what i wanted thanks, tho i have to admit if i could change things at the server side i would do it.
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.

Comments

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).

1 Comment

If you can touch the server side I agree with this solution.

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.