0

So, I have been trying for a while now and no luck. Currently I have an associative array as based on PSN profile data:

var PROFILE = {};
PROFILE.profileData = {};
PROFILE.titles = {};

and is used like this further down in the code:

PROFILE.profileData.onlineId = profileData.onlineId;
PROFILE.profileData.region = profileData.region;

PROFILE.titles[title.npCommunicationId] = title; //For looped, can be many

PROFILE.titles[title.npCommunicationId].trophies = {};
PROFILE.titles[title.npCommunicationId].trophies = trophyData.trophies; //any where from 10 - 50+ of these, for looped

Problem is, if I want to have multiple profiles, this doesn't work as it just inserts them in the same profile. I need 'PROFILE' to be an array that has all the above elements at each index.

PROFILEarray[n].profileData = {};            
PROFILEarray[n].profileData.onlineId = profileData.onlineId;
PROFILEarray[n].profileData.region = profileData.region;

Something like this is what I need^

But for the above I get this error

Cannot read property 'profileData' of undefined

Once this is complete, it's saved into a file in JSON format to then be used by PHP code I've written to insert into a db.

This is a small snippet of the json output: http://textuploader.com/5zpbk (had to cut, too bit to upload)

3
  • 1
    Have you defined PROFILEarray[n]={}? Commented Jan 22, 2016 at 5:01
  • Try with this var PROFILE = [ ]; Commented Jan 22, 2016 at 5:05
  • 1
    We need to see the whole sequence of code you tried for the array that led to that error. Probably you are just not intializing one element of the structure appropriately before trying to use it, but we can only know what that is if we see the actual code you were using when you got that error. Questions that include a detailed description of the problem AND the code that you had the problem with usually get answered pretty quickly here. Those without are much harder for people to answer. Commented Jan 22, 2016 at 5:08

1 Answer 1

1

you must define PROFILEarray[n] as object. JavaScript objects are containers for named values. You can not set value of undefined

PROFILEarray[n] is undefined in this case. Initialize it as {}(object)

Try this:

var PROFILEarray = [];
for (var n = 0; n < 5; n++) {
  PROFILEarray[n] = {};
  PROFILEarray[n].profileData = {};
  PROFILEarray[n].profileData.onlineId = n;
  PROFILEarray[n].profileData.region = 'Region' + n;
}
alert(JSON.stringify(PROFILEarray));

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

Comments

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.