-1

I need help with making a 3-dimensional array, my objective is e.g:

Just for graphic illustration :-), see row below

[category: 1[subcategories: 1[subsubcategories: 1,2],2[subsubcategories: 3,4]]

In scenario above the user has selected:

category 1
subcategories: 1
subsubcategories: 1,2
subcategories: 2
subsubcategories: 3,4

I can then with these values create a string like: 1^1:1,2^2:3,4

Hope anyone understands :)

4
  • 3
    What difficulty are you having in making a 3d array? Commented Dec 22, 2017 at 21:38
  • Why would you want pure arrays instead of creating objects that represent what you need? Commented Dec 22, 2017 at 21:39
  • Possible duplicate of three-dimensional array in javascript Commented Dec 22, 2017 at 21:39
  • I have nothing against object, I just want to collect the data the correct way :) Commented Dec 22, 2017 at 21:42

1 Answer 1

0

Use objects instead of arrays. When you use string indexes on array elements the array gets turned into an object and some of the array methods might not work properly afterwards. Why not just use an object from the beginning then.

WARNING !! If you use named indexes, JavaScript will redefine the array to a standard object. After that, some array methods and properties will produce incorrect results.

this is taken from https://www.w3schools.com.

Here is an example of how to use it:

// Object = {} instead of array = []
var myObject = {};
myObject['category'] = {1: {subcategories: {1:[1,2], 2: [3,4] }} };


// For example
var anotherObject = {};
anotherObject['category'] = {1: {}, 2: {}};
anotherObject['category'][1] = [1,2];
anotherObject['category'][2] = [3,4];

// Edit: example 3
// ---------------
// result from database JSON format
var resultFromDB = {"category": {"1": {"subcategories": {"1": {"subsubcategories": [1,2]}, "2": {"subsubcategories": [3,4] }}}} };


// example of building new object from input
var myNewObject = {};
var type;

// go through the first level 
for(var index in resultFromDB)
{
  // in case you needed this is how you would check type of input
  type = typeof resultFromDB[index];
  if((type === "object") && (type !== null)) // check if its an object
  {
    // set myNewObject[index] to new object
    myNewObject[index] = {};
    // go through second level
    for(var subIndex in resultFromDB[index])
    {
        // set myNewObject[index][subIndex] as new object
        myNewObject[index][subIndex] = {};
        // go through third level
        for(var subSubIndex in resultFromDB[index][subIndex])
        {
           // simply use an '=' to get all from that level
           myNewObject[index][subIndex][subSubIndex] = resultFromDB[index][subIndex][subSubIndex];
           
        }
    }
  }
}


console.log("This is the new object");
console.log(myNewObject);
console.log("\n");
console.log("This is the original object");
console.log(myNewObject);

// If you need to extract in multiple places you could make a function for quick access
function returnObject(incomingObject)
{
    var myNewObject = {};
    var type;
    
    // ... copy paste here all the above code from example 3 except resultFromDB
    
    return myNewObject;
}

// then just call it from anywhere
var theNewestObject = returnObject(resultFromDB);

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

4 Comments

Thanks for your anwer, could You also give an example on how to add these values with the push method? These values are fetched from a database.
@ChristerEngholm I will edit with a few examples to show you what you can do.
Thanks, I can't wait to check them out :)
@ChristerEngholm Alright, I got it. Check the code for // Example 3, that's it. If your incoming results from the database are in JSON format (they should be), this should work for you exactly as it is. Tell me if it works, if not post your incoming (from the db) data so I can see exactly what it looks like and then I can tweak the function.

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.