I've got an object with a bunch of key's and values: (I've reduced it to one value for readability)
var linksarray = {
getting_Started : "Getting Started"
};
The aim is to go through a bunch of links on my page, manipulate the href attribute and dependent on the end result, i.e. getting_Started, use that as the key to get the value 'Getting Started' from the object.
To visualize:
Here is the code, comments of values as it's being manipulated:
var link = jQuery(this).attr('href'); //link = ../getting_Started.htm
var res = link.split('/');
var lastEl = res.pop(); //link = getting_Started.htm
var testEl = firstEl.split('.');
var testElUse = testEl[0]; //link = getting_Started
testElUse = '\'' + testElUse + '\''; //link = 'getting_Started'
This all works as it should and I get the correct string that I'm looking for.
From here on I've used so many different solutions that I found on Google but none of them work. I've wasted one and a half days on this. Here is the two solutions that I expected to work...but don't.
Solution 1:
var stringJson = JSON.stringify(linksarray);
var obj=JSON.parse(stringJson) ;
alert(obj[testElUse]); //this results in 'undefined'
Solution 2:
alert(linksarray[testElUse]); //this results in 'undefined'
I know there is dot and block annotation, but as far as I understand block is the way to go if you want to dynamically reference a property according to this link: Dynamically access object property using variable
No matter what I do, I get undefined returned...not the "Getting Started" that I'm looking for.
Please help :)