I have two arrays with the following structure:
First dictionary:
[{id: 111, abbr: "FBI", name: "Federal Burreau of Investigation"},
{id: 59, abbr: "CIA", name: "Central Intelligence Agency"},
{id: 130, abbr: "EU", name: "European Union"}]
Second dictionary:
[{id: 28, name: "Administration"},
{id: 107, name: "Advocacy"},
{id: 100, name: "Agriculture"},
{id: 77, name: "Air & Aviation"}]
I am trying to create a delimiter function that accesses these objects in the following way:
finalDictionary.push({ delimiter: dictionary[0].abbr.charAt(0) });
and correspondingly:
finalDictionary.push({ delimiter: dictionary[i].name.charAt(0) });
My aim is to make the method universal and diminish the amount of final code. As you see, the only difference here is the property by which I am accessing the dictionary, 'abbr' and 'name'. I tried to do it the following way:
var ext = (name === 'agencies') ? 'abbr' : 'name';
finalDictionary.push({ delimiter: dictionary[i].ext.charAt(0) });
However, I get an error:
TypeError: Cannot read property 'charAt' of undefined
The question is how can I conditionally access the object property? Maybe, there is a better approach than this one? If not, what am I doing wrong?