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.

asked Jun 30, 2020 at 20:01
1
  • 1
    Don't use an object if ordering is important. Use an array. Commented Jun 30, 2020 at 20:03

3 Answers 3

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))

answered Jun 30, 2020 at 20:37

6 Comments

Thank you! One more thing here. How can I do to get all options before the marked one? how would it be?
@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.
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?
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
@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.
|
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.

answered Jun 30, 2020 at 20:07

Comments

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?

answered Jun 30, 2020 at 20:06

3 Comments

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].
I changed my snippet to use city, it returns [ "tipdepe", "nvldepe", "prefix", "subord" ]
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.