38

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)

rmldts
  • 366
  • 3
  • 10
Tolga E
  • 12,188
  • 15
  • 49
  • 61

5 Answers5

110

Simple solution (ES6)

You can use Object.values like this:

var valueArray = Object.values(types);

Online demo (fiddle)

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
16

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;
}
Yann
  • 2,211
  • 1
  • 15
  • 14
  • I think this is the nicest solution.. I can call makeEnum when declaring enums then just be able to call .all – Tolga E Aug 09 '13 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 Aug 12 '13 at 08: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 Aug 12 '13 at 10:14
9
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

c.P.u1
  • 16,664
  • 6
  • 46
  • 41
  • 3
    +1 for `Object.keys`. Modern JavaScript code for modern environments like node.js! – Mattias Buelens Aug 09 '13 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 Aug 12 '13 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));
musefan
  • 47,875
  • 21
  • 135
  • 185
  • 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 Aug 09 '13 at 15:49
  • 1
    Create a helper function and re-use that – musefan Aug 09 '13 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);
}
Alex K.
  • 171,639
  • 30
  • 264
  • 288