node.js 将枚举转换为值数组(将所有 JSON 值放入数组中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18150659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:16:26  来源:igfitidea点击:

Converting enums to array of values (Putting all JSON values in an array)

javascriptjsonnode.jsenums

提问by Tolga E

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

在 JavaScript 中使用Enums方法吗?在我们的代码中创建枚举..

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)

编辑:我创建了一个非常简单的枚举库来生成简单的枚举npm --save-dev install simple-enumhttps://www.npmjs.com/package/simple-enum

回答by Ali Soltani

Simple solution (ES6)

简单的解决方案 (ES6)

You can use Object.valueslike this:

你可以这样使用Object.values

var valueArray = Object.values(types);

Online demo (fiddle)

在线演示(小提琴)

回答by Yann

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

我会将地图转换为数组并将其存储为types.all. 您可以创建一个自动执行此操作的方法:

function makeEnum(enumObject){
   var all = [];
   for(var key in enumObject){
      all.push(enumObject[key]);
   }
   enumObject.all = all;
}

回答by c.P.u1

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

jsFiddle 演示

回答by musefan

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

回答by Alex K.

Juice up (or wrap) .validatesuch that it will accept typesas meaning all members?

榨汁(或包装).validate以便它接受types所有成员的意思?

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