我有两个枚举映射,并希望为枚举键创建一个数组,以删除数组中的公共子字符串,因此我也可以将其用于其他枚举映射。 枚举格式为
enum Test {
test_value_error = 1;
test_value_log = 2;
test_value_main = 3;
test_value_acknowledge = 4;}
enum Test1 {
short_set_measure = 5;
short_set_log_check = 6;
short_set_finite_check = 7;}
in this case I want to remove common substring from two enum map using a common function and convert it to array. So far I have been able to filter it to array format but can't filter out substring.
below is my code.
function toArray(enumname:object){ 返回Object.keys(obj).map(key => key.replace(/ _ / g,''));}
Still new to typescript so any help will be beneficial
答案 0 :(得分:0)
您在寻找这个吗?
enum Test {
test_value_error = 1,
test_value_log = 2,
test_value_main = 3,
test_value_acknowledge = 4
}
function toArray(enumname: object) {
return Object.keys(enumname).map(key => key.replace(/test_value/g, ""));
}
const arr = toArray(Test);
console.log(arr);
它从每个键中删除test_value
。输出看起来像这样:
[ '1', '2', '3', '4', '_error', '_log', '_main', '_acknowledge' ]
// [ '1', '2', '3', '4', 'error', 'log', 'main', 'acknowledge' ]
function toArray(enumname: object) {
return Object.keys(enumname).map(key => key.replace(/test\_value\_/g, ""));
}
// [ 'error', 'log', 'main', 'acknowledge' ]
function toArray(enumname: object) {
return Object.keys(enumname)
.filter(key => !parseInt(key))
.map(key => key.replace(/test\_value\_/g, ""));
}