I
I
Ivan Trofimov2011-11-22 14:05:31
JavaScript
Ivan Trofimov, 2011-11-22 14:05:31

Multidimensional associative array in JS

Hello.
There is a multidimensional associative array: How can this array be divided into several index arrays, which will contain elements of each sublevel. That is, for our example, there will be arrays with the following elements: arr[3] by analogy. Nesting can be even greater (that is, arr[4], arr[5], etc. will appear up to arr[10].

var Config = {
"БПИ": {
"30": {
"12": [ "2,5" ],
"24": [ "1,25" ],
"48": [ "0,65" ]
},
"60": {
"12": [ "5,0" ],
"24": [ "2,5" ],
"36": [ "1,5" ],
"48": [ "1,25" ]
},
"125": {
"12": [ "10" ],
"24": [ "5" ],
"48": [ "2,5" ]
},
"250": {
"12": [ "20,0" ],
"24": [ "10,0" ],
"48": [ "5,0" ]
}
}
};


arr[0] = ["БПИ"];
arr[1] = ["30","60","125","250"];
arr[2] = ["12","24","48","12","24","36","48","12","24","48"];

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoly, 2011-11-22
@taliban

habrahabr.ru/qa/13732/#answer_57959
I still advise you to take a closer look here. An excellent solution to your problem.

S
sdevalex, 2011-11-22
@sdevalex

Recursion? Not?

function findKeys(object, result){
     result = result || [];

     var keys = [];

     $.each(object, function(index, value){
           keys.push(index);

           if($.isObject(value))
                findKeys(object, result);
     });

     result.push(keys);

     return result;
}

var data = findKeys(Config);

S
sdevalex, 2011-11-23
@sdevalex

Version without jQuery...

function findKeys(object, result){
     result = result || []; //Это как необязательный параметр... читайте так findKeys(object, result = [])

     var keys = [];

     for(var i in object){
           if(!object.hasOwnProperty(i))
                continue;

           keys.push(index);

           if($.isObject(value))
                findKeys(object, result);
     };

     result.push(keys);

     return result;
}

var data = findKeys(Config);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question