0

I have the following javascript object that represents columns of a table that I want user can select to show and analyze based on his/her selection.

var nominalVars   = {
    state     : 'State',    
    messoreg  : 'Messoreg',
    reggeoint : 'RegGeoInt',
    reggeoimd : 'RegGeoImed',
    microreg  : 'Microreg',
    regmetro  : 'RegMetro',
    city      : 'City',
    tipdepe   : 'TipDep',
    nvldepe   : 'NvlDepe',
    prefix    : 'Prefix',
    subord    : 'Subord'
    };

What I need is to retrieve all the properties bellow the last checkbox checked, e.g., If the user selects [state] checkbox, then I want to get [messoreg], [reggeoint], [reggeoimd], [microreg], etc. and them passing the values to an Array.

I don't know if there is it another way to do that but I'm trying to avoid using Swith of If statements.

Marcio Lino
  • 379
  • 2
  • 15

3 Answers3

2

You should never use an object if you want to keep track of the order. You can use a Map or an Array. The Map object holds key-value pairs and remembers the original insertion order of the keys.

let map = new Map();
map.set('state','State');
map.set('messoreg','Messoreg');
map.set('reggeoint','RegGeoInt');
map.set('reggeoimd','RegGeoImed');
map.set('microreg','Microreg');
map.set('regmetro','RegMetro');
map.set('city','City');
map.set('tipdepe','TipDep');
map.set('nvldepe','NvlDepe');
map.set('prefix','Prefix');
map.set('subord','Subord');

function getKeys(map, afterKey) {
  let shouldAdd = false, res = [];
  for (const key of map.keys()) {
    if (shouldAdd) res.push(key);
    if (key === afterKey) shouldAdd = true;
  }
  return res;
}

function getKeysBefore(map, beforeKey) {
  let shouldAdd = true, res = [];
  for (const key of map.keys()) {
    if (key === beforeKey) shouldAdd = false;
    if (shouldAdd) res.push(key);
  }
  return res;
}

//get keys after 'reggeoint'
let keys;
keys = getKeys(map, 'reggeoint');
document.write(JSON.stringify(keys))

//get keys after 'city'
keys = getKeys(map, 'city');
document.write(JSON.stringify(keys))

//get keys before 'city'
keys = getKeysBefore(map, 'city');
document.write(JSON.stringify(keys))
rishabh0211
  • 433
  • 4
  • 10
  • Thank you! One more thing here. How can I do to get all options before the marked one? how would it be? – Marcio Lino Jun 30 '20 at 23:59
  • @user1460038 To get the keys before a particular key, you can modify the `getKeys` function a bit. I've added the `getBeforeKeys` function in the answer. – rishabh0211 Jul 01 '20 at 07:44
  • In the case, if I need to retrieve both key and value to separate arrays I would need to create another array like "values[];" and use values.push(key)? That's correct? – Marcio Lino Jul 01 '20 at 11:57
  • In the case, if I need to retrieve both key and value to separate arrays? I have tried like this ` for (var [key, value] of map) {console.log(key + " = " + value); ` but doesn't work – Marcio Lino Jul 01 '20 at 12:12
  • @user1460038 The way you mentioned above is correct if you want to access both keys and values. Destructure it by `for(var [key, value] of map)` and use key and value in the loop. – rishabh0211 Jul 01 '20 at 12:22
  • Yeap, work flawlessly. Thank you so much for your help. – Marcio Lino Jul 01 '20 at 15:00
0

Use Object.keys() to get the keys of an obect as an array. Then you can use indexOf() to get the index of a specific property, and you can slice the rest of the keys.

var nominalVars = {
  state: 'State',
  messoreg: 'Messoreg',
  reggeoint: 'RegGeoInt',
  reggeoimd: 'RegGeoImed',
  microreg: 'Microreg',
  regmetro: 'RegMetro',
  city: 'City',
  tipdepe: 'TipDep',
  nvldepe: 'NvlDepe',
  prefix: 'Prefix',
  subord: 'Subord'
};
let last_checkbox = "city";
let keys = Object.keys(nominalVars);
let index = keys.indexOf(last_checkbox);
let following_keys = keys.slice(index+1);
console.log(following_keys);

Note that only ES6 requires that the order of object properties to be maintained:

Does ES6 introduce a well-defined order of enumeration for object properties?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I tried your suggestion here. It seems it only will work if I select the last option, in this case, state. If I choose [city] option, for example, the code bring me all the items above it, such as [regmetro], [microreg], and so on up to [state] option. I need to get only the option bellow city, such as [tipdepe], [nvldepe], [prefix] and [subord]. – Marcio Lino Jun 30 '20 at 21:34
  • I changed my snippet to use `city`, it returns `[ "tipdepe", "nvldepe", "prefix", "subord" ]` – Barmar Jun 30 '20 at 21:43
  • JavaScript doesn't require that `Object.keys()` return the keys in the same order that you defined them. You should use the array or `Map` solution. – Barmar Jun 30 '20 at 21:47
0
Object.keys(obj)

Will return the defined keys for any javascript-object. You'll have to filter this array accordingly to match your requirements.

genius42
  • 243
  • 1
  • 6