46

I use this method Enums in JavaScript? to create enums in our code..

So

var types = {
  "WHITE" : 0,
  "BLACK" : 1
}

Now the issue is when I want create validations anywhere, I have to do this;

model.validate("typesColumn", [ types.WHITE, types.BLACK ]);

Now is there a way I can just simple convert the values in types to an array so that I don't have to list all of the values of the enum?

model.validate("typesColumn", types.ValuesInArray]);

EDIT: I created a very simple enum library to generate simple enums npm --save-dev install simple-enum (https://www.npmjs.com/package/simple-enum)

1
  • This is not enum. This is just an object.
    – Yogesh G
    Commented Jul 3 at 5:03

5 Answers 5

120

Simple solution (ES6)

You can use Object.values like this:

var valueArray = Object.values(types);

Online demo (fiddle)

1
  • If the enum is a string, (ex. enum { "Hello", "Goodbye" } then Object.keys(enum) works.
    – C RICH
    Commented Nov 7, 2023 at 17:52
17

I would convert the map into an array and store it as types.all. You can create a method that does it automatically:

function makeEnum(enumObject){
   var all = [];
   for(var key in enumObject){
      all.push(enumObject[key]);
   }
   enumObject.all = all;
}
3
  • I think this is the nicest solution.. I can call makeEnum when declaring enums then just be able to call .all
    – Tolga E
    Commented Aug 9, 2013 at 16:48
  • Does this even do what is required? Surely after this all would be and array of property names? like ['WHITE', 'BLACK'] rather than an array of values (which the OP has requested). Also, enum is a reserved word! Why was this accepted?...
    – musefan
    Commented Aug 12, 2013 at 8:20
  • I guess the OP took the idea without looking into the exact implementation that is indeed incorrect. I will correct it for other users, thanks for the comment.
    – Yann
    Commented Aug 12, 2013 at 10:14
10
var types = {
  "WHITE" : 0,
  "BLACK" : 1
}
var typeArray = Object.keys(types).map(function(type) {
    return types[type];
});
//typeArray [0,1]

model.validate("typesColumn", typeArray);

jsFiddle Demo

2
  • 3
    +1 for Object.keys. Modern JavaScript code for modern environments like node.js! Commented Aug 9, 2013 at 16:06
  • +1, but I'd rather refactor keys + map to a general function values(obj) and then simply validate("typesColumn", values(types)).
    – georg
    Commented Aug 12, 2013 at 10:24
2

You could convert it to an array, or you can just iterate the properties of the object (which is how you would create the array anyway):

for(var i in types){
    var type = types[i];
    //in the first iteration: i = "WHITE", type = 0
    //in the second iteration: i = "BLACK", type = 1
}

Just for completeness, you can create the array with that method as follows:

var arr = [];
for(var i in types){
    var type = types[i];
    arr.push(type);
}
//arr = [0, 1]

to make this reusable you could create a helper function:

function ObjectToValueArray(obj){
    var arr = [];
    for(var i in obj){
        var v = obj[i];
        arr.push(v);
    }
    return arr;
}

Which can be called like so:

model.validate("typesColumn", ObjectToValueArray(types));
2
  • I know that I can convert these but since these are used in various places, I'm looking for a one line solution. Something very simple that supports this functionality
    – Tolga E
    Commented Aug 9, 2013 at 15:49
  • 1
    Create a helper function and re-use that
    – musefan
    Commented Aug 9, 2013 at 15:51
1

Juice up (or wrap) .validate such that it will accept types as meaning all members?

var types = {
  "WHITE" : 0,
  "BLACK" : 1,
  "RED"   : 200
}

validate("foo", types.WHITE); // 0
validate("foo", [types.WHITE, types.BLACK]); // 0,1 
validate("foo", types); // 0,1,200

function validate(foo, values) { 
    var arr = [];
    switch (typeof values) {
        case "number":
            arr.push(values);
            break;
        case "object":
            if (values instanceof Array) 
                arr = values;
            else {
                for (var k in values)
                    arr.push(values[k]);
            }
            break;
    }
    alert(arr);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.