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

Taplar
  • 24,788
  • 4
  • 22
  • 35

1 Answers1

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);
Ivan86
  • 5,695
  • 2
  • 14
  • 30
  • 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. – Christer Engholm Dec 23 '17 at 23:52
  • @ChristerEngholm I will edit with a few examples to show you what you can do. – Ivan86 Dec 24 '17 at 00:06
  • Thanks, I can't wait to check them out :) – Christer Engholm Dec 24 '17 at 00:16
  • @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. – Ivan86 Dec 24 '17 at 02:01