Jump to content

How to convert multidimensional array into java script object?


Recommended Posts

I have a multidimensional array that I create dynamically like this

				var pairs = new Array();

				pairs.push({person:"joel",fruit:"peach"});

				pairs.push({person:"amina",fruit:"mangoe"});

				pairs.push({person:"peter",fruit:"mangoe"});
So basically, the array has several elements each of which has two child elements, the first having key "peoson" and the second having key "fruit" such that pairs[0].fruit would have value "peach" for example and pairs[2].person would be "peter"
 
 
Now I want to convert this array into a global javascript object so that I can access members of the array anywhere from my script.
 
The object should look something like so:
 

var NS.harvest = {

0:{person:"joel",fruit:"peach"},

1:{person:"amina",fruit:"mangoe"},

2:{person:"peter",fruit:"mangoe"}

}

So I tried this:

var NS.harvest = {};

for (var i = 0; i < pairs.length; ++i){

    if(pairs[i] !== undefined){

       NS.harvest[i] = pairs[i];

    }

}

return NS.harvest;

That didn't do the trick.  What should I do?  And what do I do if I need to convert the object back into the array?

 

Copying from pairs? Any reason you can't just

NS.harvest = pairs;
But are you executing this from a function? Either have the function return pairs, or forget the function and just access NS.harvest (after setting it) directly.

I've just done something similar and from my understanding javascript multi-dimensional arrays become javascript objects. 

 

I have attached the code that I did, might be some help to you. This gave me an object / multi-dimensional array like PHP;

 

data =>

          array(1 =>

                           array( id => 1,

                                     code => some code,

                                     desc => description,

                                     surveyDate => 27/11/2013,

                                     building => array( buildingCode => array( total => 150.00,

                                                                                                       count => 5,

                                                                                                       buildingID => 7,

                                                                                                       buildingCode => buildingCode,

                                                                                                       building => building description

                                                                                                     )

                                                                  )

 

          )

);

                                    

Hope that helps

        var data = new Array();
        
        for (var i=0; i < surveys.length; i++) {
            
            var row = surveys.item(i);
            
            /** format any null data to something more useful */
            var buildingCode = (row.buildingCode != null) ? row.buildingCode : 
                                                            'Site Only'; 
                                                    
            var buildingID = (row.buildingID != null) ? row.buildingID : 0;
            
            
            /** if condition survey code is not the same as the previous then 
             *  reset the data for usage */
            if (row.code != preRowCode) {
                
                preRowCode = row.code;
                prevBuildingCode = '';
                var survey = new Array();
                survey['building'] = new Object();
                
            }
            
            /** if the building hasn't been set or is different then set the 
             *  prelimanary data */
            if (buildingCode != prevBuildingCode) {
                
                prevBuildingCode = buildingCode;
                
                survey.building[buildingCode] = {
                    total: row.totalCost,
                    count: 1,
                    buildingID: buildingID,
                    buildingCode: buildingCode,
                    building: row.buildingDesc
                };
                
            } else {
                
                survey.building[buildingCode].total += row.totalCost;
                survey.building[buildingCode].count += 1;
            }
            
            survey['id'] = row.id;
            survey['code'] = row.code;
            survey['siteCode'] = row.siteCode;
            survey['surveyDate'] = row.surveyDate;
            
            /** assign the survey array to the data array, js crazy way of doing
             *  a multi-dimensional array */
            data[row.id] = survey;
        }
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.