Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)

share|improve this question

4 Answers 4

up vote 2 down vote accepted

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;
}
share|improve this answer
    
I think this is the nicest solution.. I can call makeEnum when declaring enums then just be able to call .all –  Tolga E Aug 9 '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 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 Aug 12 '13 at 10:14
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

share|improve this answer
1  
+1 for Object.keys. Modern JavaScript code for modern environments like node.js! –  Mattias Buelens Aug 9 '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

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));
share|improve this answer
    
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 9 '13 at 15:49
1  
Create a helper function and re-use that –  musefan Aug 9 '13 at 15:51

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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