-2

I have the following object:

const o = {
  f_Auth: 0,
  f_Assoc: 0,
  f_ReAssoc: 0,
  f_EAP: 0,
  f_EAPOL: 0,
  f_DHCP: 0,
  f_Data: 0,
  f_Unk: 0,
  f_test1: 0,
  f_test2: 0,
  f_testlevel: 0,
  f_testsubtype: ""
};

I want to increment a particular array element value by referencing it through an “index” or “position”.

How do I do that?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75

1 Answers1

1

Keys in Javascript objects are not ordered, in other words there's no difference betweeen

a = {x:1, y:2};

and

b = {y:2, x:1};

except for the order of evaluation when building the objects (if for example instead of 1 and 2 the statements were using f() and g() then in one case f() would be called before g() and the opposite in the other case).

Note that you may get a different key order from Object.keys(obj) for example, but this is not guaranteed to work always and the same in all Javascript engines.

To access the keys by index you need to do this explicitly... for example:

keys = ["f_Auth", "f_Assoc", "f_ReAssoc", "f_EAP",
        "f_EAPOL", "f_DHCP", "f_Data", "f_Unk", "f_test1",
        "f_test2", "f_testlevel", "f_testsubtype"];

...

obj[keys[index]]++;
6502
  • 112,025
  • 15
  • 165
  • 265