I am trying to access JavaScript objects that are stored in a multi-dimensional array, which is being exported by a WordPress plug-in. Unfortunately, I cannot make changes to the code to use a single array.
There are two arrays called "employees". Is this array format compatible with JavaScript? The JSON export was originally intended for PHP processing.
(Please note that the code provided below is a simplified model used to demonstrate the issue).
var data = '{"employees":[{"firstName":"John0"}, {"firstName":"Anna0"},{"firstName":"Peter0"}],"employees":[{"firstName":"John1"}, {"firstName":"Anna1"},{"firstName":"Peter1"}]};';
var json = JSON.parse(data);
document.querySelector('#test').innerHTML = json.employees[2].firstName;
Check it out on JSFiddle:
https://jsfiddle.net/2524fhf4/11/
For example, how would one go about accessing the value "Peter0" in the first array? In a single array, it could be accessed as shown below:
var result = json.employees[2].firstName;
It seems like in this current format, we can only access the values from the last array.