How to access array key with another variable?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
12 views
Hi all. Is an associative multidimensional array

var sedan = { 'eject' : { '13' : 50, '14' : 50, '15' : 50 }, 'installation' : { '13' : 25, '14' : 40, '15' : 50 }, 'balancing' : { '13' : 50, '14' : 60, '15' : 80 } }, crossover = { 'eject' : { '13' : 50, '14' : 60, '15' : 60 }, 'installation' : { '13' : 50, '14' : 60, '15' : 60 }, 'balancing' : { '13' : 444, '14' : 555, '15' : 666 } }, truckPrice = 370;


and a function to get the current value of the selected machine. In bodyRes hits : 'sedan' , 'crossover' or truck', which is equivalent to the key names of the associative array.

var bodyRes = bodyCheck(); bodyCheck function() { if (body == 'Sedan') { x = 'sedan'; } else if (body == 'Crossover') { x = 'crossover'; } else if (body == 'Truck') { x = 'truck'; } return x; }


Next, I want to compare the type of body of the car ( 'sedan' , 'crossover' or truck') and refer to the corresponding array key, then the service ('eject', 'installation' or 'balancing') and add the selected wheel diameter

sedan['maintenance'][diameter]
bodyRes [ 'service' ] [ 'diameter' ] = number - the key value of an array

var balancingSum = balancingCheckSum(); balancingCheckSum function() { var x = bodyRes; console.log( bodyRes ); console.log( x['maintenance'][diameter] ); return x; }; console.log(balancingSum); - x is undefined


Selected diameter is passed OK and I can access the array key and get value, substituting [diameter] and returns, for example, 50 or 80
But how to substitute back the function result bodyCheck() in a variable bodyRes ? BalancingSum variable to return only the number is already pre-selected values of the array, depending on what the user selected ?

sedan['maintenance'][diameter]
crossover['maintenance'][diameter]

Thank you
by | 12 views

2 Answers

0 like 0 dislike
Is an associative multidimensional array

No, you have several objects in different variables, not one array.

Accordingly, the decision to collect data in a suitable structure. In this case, the object will be:
var data = { sedan: { 'eject' : { '13' : 50, '14' : 50, '15' : 50 }, 'installation' : { '13' : 25, '14' : 40, '15' : 50 }, 'balancing' : { '13' : 50, '14' : 60, '15' : 80 } }, crossover: { 'eject' : { '13' : 50, '14' : 60, '15' : 60 }, 'installation' : { '13' : 50, '14' : 60, '15' : 60 }, 'balancing' : { '13' : 444, '14' : 555, '15' : 666 } }, };


Well and further all is simple:
data[x].balancing
by
0 like 0 dislike
var cars = { sedan: { ... }, crossover: { ... }, truck: { ... } }; car = cars['sedan'];
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
3 answers
0 like 0 dislike
2 answers
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
35,392 users