Object.values 似乎不适用于枚举类型
Object.values seems not to work in enum type
我在我的应用程序中定义了这个枚举:
export enum Status {
BOOKED = 'B',
FREE = 'F',
}
然后我在控制台上添加这条消息
console.log ('<------------------------------------>');
console.log (code.value);
console.log (Object.values(Status));
console.log (code.value in Object.values(Status));
console.log ('<------------------------------------>');
<------------------------------------>
我在控制台上看到了这个,code.value 不包含在枚举中;我应该看到 true
B
[ 'B', 'F' ]
false
你有这个对象
export enum Status {
BOOKED = 'B',
FREE = 'F',
}
Object.values(Status)
会给你 [ 'B', 'F' ]
这是预期的
阅读此内容了解更多信息 -
The Object.values() method returns an array of a given object's own
enumerable property values, in the same order as that provided by a
for...in loop. (The only difference is that a for...in loop enumerates
properties in the prototype chain as well.)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
您应该使用 array.include()
来检查数组是否包含值 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
const values = Object.values(Status);
console.log(values.includes(code.value));
我在我的应用程序中定义了这个枚举:
export enum Status {
BOOKED = 'B',
FREE = 'F',
}
然后我在控制台上添加这条消息
console.log ('<------------------------------------>');
console.log (code.value);
console.log (Object.values(Status));
console.log (code.value in Object.values(Status));
console.log ('<------------------------------------>');
<------------------------------------>
我在控制台上看到了这个,code.value 不包含在枚举中;我应该看到 true
B
[ 'B', 'F' ]
false
你有这个对象
export enum Status {
BOOKED = 'B',
FREE = 'F',
}
Object.values(Status)
会给你 [ 'B', 'F' ]
这是预期的
阅读此内容了解更多信息 -
The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop. (The only difference is that a for...in loop enumerates properties in the prototype chain as well.)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
您应该使用 array.include()
来检查数组是否包含值 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
const values = Object.values(Status);
console.log(values.includes(code.value));