Waymas
Waymas

Reputation: 423

jquery each map array not printing all the content

I have these two objects serving as key/value maps. The first map contains two different dates that I will compare later, and the other map contains a link and an image,

var estelaresfechas = {
    '2012-01-02': '2012-12-02',
    '2012-01-02': '2012-12-02',
    '2012-01-02': '2012-12-02'
}
//Link e imagen
var est_link_img = {
    '' : 'estelarAticulosEscolares_03082012.jpg',
    'google.com' : 'estelarCuadernoProfesional_03082012.jpg',
    '' : 'estelarLaptop_03082012.jpg'
}

$.each(estelaresfechas,function(key,val){

                $.each(est_link_img,function(index,value){
                    var fechainiest = Date.parse(key);
                     var fechafinalest = Date.parse(val);
                     var fechainicomest = Date.today().compareTo(fechainiest);
                     var fechafinest = Date.today().compareTo(fechafinalest);
                    if(fechainicomest == 1 && fechafinest == -1){


                        $("#slider").append("<a href='"+ index +"'><img src='img/" + value+ "'/></a>");

                    }
                    else{
                        console.log("nada")
                    }
                });




    })

The first thing it does is that it compares the dates and if the dates are on the range it will append it to a div with his link and image (the link is optional), but for some reason it doesn't print the first one. Any ideas?

Upvotes: 0

Views: 342

Answers (1)

user229044
user229044

Reputation: 239250

Both of these objects contain duplicate keys:

var estelaresfechas = {
    '2012-01-02': '2012-12-02',
    '2012-01-02': '2012-12-02',
    '2012-01-02': '2012-12-02'
}

var est_link_img = {
    '' : 'estelarAticulosEscolares_03082012.jpg',
    'google.com' : 'estelarCuadernoProfesional_03082012.jpg',
    '' : 'estelarLaptop_03082012.jpg'
}

When you specify the same key, the last one wins.

To simplify, writing this...

{ 
   "a": "1",
   "a": "2",
   "a": "3"
}

...is equivalent to writing this:

{ "a": "3" }

You need to choose unique names for your keys.

Upvotes: 1

Related Questions