0

I'm actaully out of ideas. I've been reading around about this problem but I didn't find any answer that suits my need. So here is the scenario

I have some inputs where I get a value by their id

<input type="hidden" id="name" value="Taco house" />
<input type="hidden" id="pin" value="Mexican" />

And I try to use the value as index to build an array

var name = $('#name').val();
var pin = $('#pin').val();

markersData = {
    pin: [ //here it should put 'Mexican' instead it puts 'pin'
    {
        name: name,
        location_latitude: 43.5391318, 
        location_longitude: 10.3020171,
        //other nodes here
    }
    ]
};

If I print pin variable before passing to the array it prints its actual value, but once I pass to the array the value is lost and the index became pin.

For sure this is an obvious question but since I'm not that good in jquery I need some help.

5
  • 2
    Can you post your full code ? Commented Oct 3, 2016 at 13:40
  • 2
    Your problem is not at all clear, but have you tried var foo = markersData.pin[parseInt(pin, 10)] Commented Oct 3, 2016 at 13:40
  • Could you post some more info? hard to understand the problem from your description Commented Oct 3, 2016 at 13:42
  • You do not have a pin with attribute pin. You have a named pin. So what is name:name looking like? name:"Taco house" ? Commented Oct 3, 2016 at 13:44
  • @mplungjan yes actually name look like name:"Taco house" Commented Oct 3, 2016 at 13:45

1 Answer 1

2

This should do the trick :

var name = $('#name').val();
var pin = $('#pin').val();
markersData = {};
markersData[pin] = [{
    name: name,
    location_latitude: 43.5391318,
    location_longitude: 10.3020171,
    //other nodes here
}];

Your way won't work, because the parser consider pin as a string, not as the content of your "pin" variable. Neither can you do markersData.pin for the same reason.

As far as I know (I might be wrong), the only way to do this is to use square brackets, which might be a bit confusing since it looks like adding an index to an array. Hope this helps

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

17 Comments

Is markersData[pin] the same as markersData.pin? Please explain what you are actually doing here to help the asker understand why this should "do the trick".
@Zack, I was actually updating my answer to add some clarification. It should also answer your question.
This is actually working but I don't understand why I cannot use variable to build an array index in my previous way though
@Fabio Someone can provide you with a better explanation, sinceI am not versed in javascript inner working, but from what I understand, the parser does not see pin as a variable when you use it as an object keys. I think it is intended to prevent confusion, JS can't know if you want to use a string or a variable you declared.
I see, i'm not expert too, i know it should work properly in php
|

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.