简体   繁体   中英

Node.js array instantiation

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)

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)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM