将枚举转换为值数组(将所有JSON值放入数组中) [英] Converting enums to array of values (Putting all JSON values in an array)
问题描述
我使用此方法 JavaScript中的枚举?在我们的代码中创建枚举..
I use this method Enums in JavaScript? to create enums in our code..
所以
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]);
编辑:我创建了一个非常简单的枚举库来生成简单的枚举npm --save-dev install simple-enum
( https://www.npmjs.com/package/simple-enum )
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)
推荐答案
我会将地图转换成数组并将其存储为types.all
.
您可以创建一种自动执行此操作的方法:
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;
}
这篇关于将枚举转换为值数组(将所有JSON值放入数组中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!