0

I was building something with google maps and I wrote this piece of code to fill the infowindows:

  mouseover: function(marker, event, context){

    var numManc = context.data.num_manc;
    var elencoManc = '';

    for(i=1;i<=numManc;i++)
    {
        var mymanc = i;
        elencoManc = elencoManc + context.data.mancinita.mymanc.mancinita;
    }

   etc....

I alsway get this error TypeError: context.data.mancinita.mymanc is undefined.

Actually what I need is that the var "elencoManc" concatenate itself N times, where N equals the var "numManc".

So in the "for" loop I should get something like

elencoManc = elencoManc + context.data.mancinita.1.mancinita;
elencoManc = elencoManc + context.data.mancinita.2.mancinita;
elencoManc = elencoManc + context.data.mancinita.3.mancinita;


elencoManc = elencoManc + context.data.mancinita.N.mancinita;

It seems that I cannot make that concat dynamic.

This is one record stored in the array:

{
                        lat: 35.110901,
                        lng: 12.876027,
                        options: {
                            icon: "icon_2.png",
                            },
                        data: {
                            nome: "Name of the sotre",
                            indirizzo: "Address of the sotre",
                            city: "Rome",
                            linkStore: "http://www.storelink.com",
                            immagine_copertina: "asset/imageStore.png",
                            city: "Rome",
                            num_tipi: 1,
                            num_manc: 2,
                            tipi_1: {
                               icona_tipo: "asset/ico/ico2.png",
                               tipo: "negozio_bio"
                            },
                            mancinita: {
                               1: {
                                    icona: "iconManc2.png",
                                    idmanc: 2,
                                    mancinita: "organic"
                                  },
                               2: {
                                    icona: "iconManc5.png",
                                    idmanc: 5,
                                    mancinita: "lowFat"
                            },

                           },
                          distanza: "0.000"
                          }
                         }

Any suggestions? Thanks

4
  • 3
    elencoManc = elencoManc + context.data.mancinita[i].mancinita; Commented Sep 8, 2014 at 10:17
  • @Donal thanks. Why am I getting this error now? SyntaxError: missing name after . operator Commented Sep 8, 2014 at 10:30
  • Not sure. But you have a duplicate key in your JSON. city: "Rome" appears twice! Commented Sep 8, 2014 at 10:35
  • your first suggestion about the array index was correct. Thanks again. By the way there was an exceding comma in json that caused the system to crash. It wasn't the "city=rome" duplicate. Commented Sep 8, 2014 at 14:29

1 Answer 1

1

There are some issues with your JSON. For example city: "Rome" appears twice. If you fix them, then something like this should work (assuming context is the same as your JSON):

context.data.mancinita[1].mancinita

I have created a fiddle here with an example to show you:

var context = {
                        lat: 35.110901,
                        lng: 12.876027,
                        options: {
                            icon: "icon_2.png"
                            },
                        data: {
                            nome: "Name of the sotre",
                            indirizzo: "Address of the sotre",
                            city: "Rome",
                            linkStore: "http://www.storelink.com",
                            immagine_copertina: "asset/imageStore.png",
                                                        num_tipi: 1,
                            num_manc: 2,
                            tipi_1: {
                               icona_tipo: "asset/ico/ico2.png",
                               tipo: "negozio_bio"
                            },
                            mancinita: {
                               1: {
                                    icona: "iconManc2.png",
                                    idmanc: 2,
                                    mancinita: "organic"
                                  },
                               2: {
                                    icona: "iconManc5.png",
                                    idmanc: 5,
                                    mancinita: "lowFat"
                            }
                           },
                          distanza: "0.000"
                          }
                         };


console.log(context.data.mancinita[1].mancinita);
Sign up to request clarification or add additional context in comments.

1 Comment

Damn! that was it! Thank you very much!

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.