An array of objects contains a key "dataTypes" with many data types fetched from the backend . All these dataTypes should be categorized into four segments number, string, date, boolean . Since there are many dataTypes I am figuring out having an object mapping with key allows regexp .
const data = [
{
"title": "id",
"dataTypes": "character varying(65535)"
},
{
"title": "value",
"dataTypes": "int"
},
{
"title": "number_value",
"dataTypes": "bigint"
},
{
"title": "first_name",
"dataTypes": "varchar"
},
{
"title": "last_name",
"dataTypes": "char"
},
{
"title": "activated_date",
"dataTypes": "date without timestamp"
},
{
"title": "selected",
"dataTypes": "boolean"
},
]
const objectMap = {
"char" : "string",
"varchar" : "string",
"character varying(65535)" : "string",
"int" : "number",
"numeric" : "number",
"bigint" : "number",
"boolean" : "boolean",
"date" : "date",
"date without timestamp" : "date"
};
data.map(el => el.dataTypes = objectMap[el.dataTypes])
console.log(data)
since I need to add more keys in the object, is there anyway I could use regexp as keys
const objectMap = {
.*char.* : "string"
}
Expected result :
const data = [
{
"title": "id",
"dataTypes": "string"
},
{
"title": "value",
"dataTypes": "int"
},
{
"title": "number_value",
"dataTypes": "int"
},
{
"title": "first_name",
"dataTypes": "string"
},
{
"title": "last_name",
"dataTypes": "string"
},
{
"title": "activated_date",
"dataTypes": "date"
},
{
"title": "selected",
"dataTypes": "boolean"
},
]