Array value

I need to get one of the 5 values in the array below to give my variable a value (to multiply with later).
shoes will get one of the values from somewhere else and be set as that (i.e. a1g1r1 )

let shoes = “”;
let xaxis = {
a1g1r1: [ 30, 30, 15, 15, 10 ],
a1g1r2: [35, 35, 15, 10, 5],
a1g1r3: [35, 35, 5, 20, 5],
a1g1r4: [40, 40, 5, 10, 5],
a1g1r5: [45, 45, 0, 10, 0],

var a = xaxis5;

Any advice guys?..

Hey tiaan,

Glad to be in touch with you - seems like you’re always working on something interesting.

I played around with your code and with a couple minor adjustments and this is what you need:

let shoes = "a1g1r2”; // just for the example
let xaxis = {
  a1g1r1: [30, 30, 15, 15, 10],
  a1g1r2: [35, 35, 15, 10, 5],
  a1g1r3: [35, 35, 5, 20, 5],
  a1g1r4: [40, 40, 5, 10, 5],
  a1g1r5: [45, 45, 0, 10, 0],
}
var a = xaxis[shoes][4]; // a will get the value 5

Your original code had this:

var a = xaxis[5](shoes);

This gets interpreted as a function because of the parentheses: (shoes)

So, as you can see in my code, I got rid of the parenthesis so that xaxis is treated as an array, and I put shoes as the first index, or key.

Then, keep in mind that in Javascript, a numeric index needs to be zero-based. So try this:

var a = xaxis[shoes][0]; // a will get the value 35

To get the whole array of values shoes:

var arr = xaxis[shoes];

Using the example above:

let shoes = "a1g1r2”;
var arr = xaxis[shoes];
// arr gets the array:  [35, 35, 15, 10, 5]

If you want just one specific value from the array:

let shoes = "a1g1r2”;
var arr = xaxis[shoes];
var value= arr[4];
// value = 5

And putting it all together in one line (as in the code example above):

let shoes = "a1g1r2”;
var a = xaxis[shoes][4]; // a will get the value 5
// a = 5 

I hope this helps.

See you around the forum,
Yisrael

Hi Yisrael

Thank you kindly, I can learn a lot from this!

It seems I regularly get stuck at the minor details… I’m confident my learning curve will increase as we continue with projects.

I would be interested to see some of your work!

Cheers
Tiaan

Hi tiaan,

Glad I was able to help.

Cheers indeed,

Yisrael

Hi Yisrael

I’m busy with something quite interesting and was wondering if I could impress on your help?

Cheers
Tiaan

Hey tiaan,

You’ve piqued my interest. Start a new thread and I’ll see if I can help.

Yisrael

Many thanks Yisrael! I’ve created a new post