0

I have an array of objects, something like this:

var details=[{month:  1, equips:32.1, instals:12.6, softs:  6.7, manuts:6.2,  formacs:  9.7, total:0.0},    {mes:  2,       equips:37.5, instals:14.1, softs:  7.5, manuts:5.7,  formacs:  8.7, total:0.0}];

And to aid me I created a new array "headers" that get the objects headers, something like this function that creates the array I want:

var headers=[]
function getHeaders(){
                var i,cab;
                for(cab in details[0]) headers.push(cab);
            }

And I get an array, something like this headers=[month, equips, instals, softs, manuts, formacs, total], and it is working, because if I call, for instance, headers[2] I get "instals".

My may goal was to call something from "details" array with something like this:

details[0].headers[2] and get the value : 12.6 instead of using details[0].instals

any help?

Amir
  • 1,328
  • 2
  • 13
  • 27

2 Answers2

0

You could use brackets instead of dot notation.

like so: details[0][headers[2]]

Raquel Guimarães
  • 957
  • 1
  • 12
  • 18
  • Thank you (obrigada)... it worked ... btw , i need to change the value so that i get only one decimal ... and i tried ( i and j are part of a "for" cycle) [details[i][headers[j]].toFixed(1), but i get "this is not a function"... – Nuno Marques Jun 15 '17 at 12:23
  • de nada :) This question might help with the rounding: https://stackoverflow.com/questions/7342957/how-do-you-round-to-1-decimal-place-in-javascript – Raquel Guimarães Jun 15 '17 at 12:25
  • yep, that worked, thx... actually what i did was correct, but i'ved forgot that my first value was a text and not a number, so it was getting a "not a function" because of that. – Nuno Marques Jun 15 '17 at 13:11
0

You could directly get the value using:

Object.values(details[0])[0];//1 (month)
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151