I am trying to figure out how to properly parse some values returned from the linkedIn JavaScript API.
At present I am using the following code to loop through and print the objects returned.
for(var position in profile.positions)
{
profHTML = profHTML + profile.positions[position];
}
The result (which is getting me part of the way to figuring it out is:
11[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Essentially its the # of objects (which seems to be the first thing returned) and then each of the objects.
According to what is returned when I do a console log in Chrome this code is returning an array of objects which contain an object named "company". Company contains 4 attributes ("id","industry","name","type")
I gather that profile.positions[position] is the reference to the element returned however I am not sure of the syntax to access the company object and attribute while looping.
I am also not sure what would be the best practice to avoid trying to reference the field on the very first loop which returns the number of objects.
Thanks in advance for your help.
To start, use a regular for
loop, not a for...in
loop, to iterate over an array.
var positions = profile.positions,
position;
for(var i=0; i<positions.length; i++)
{
position = positions[i];
profHTML = profHTML + position;
}
Now, to access one of the properties of the position
, just use a member operator (.
or []
):
var positions = profile.positions,
company;
for(var i=0; i<positions.length; i++)
{
company = positions[i].company;
console.log(company.id, company.industry, company.name, company.type);
}