简体   繁体   中英

Mapping the object keys with regexp with javascript

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"
        },
]

You can use another array with all the possible replacement values and a matching criteria - which can then be a string or a regular expression (or a function or ...)

 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 replacements = [ { criteria: /.*char.*/, replacement: "string" }, { criteria: "int", replacement: "number" }, { criteria: "numeric", replacement: "number" }, { criteria: "bigint", replacement: "number" }, { criteria: "boolean", replacement: "boolean" }, { criteria: /date.*/, replacement: "date" } ]; data.forEach(d => { const needle = replacements.find(r => { if (typeof r.criteria === "string") { return d.dataTypes === r.criteria; } else { return r.criteria.test(d.dataTypes); } }); d.oldDataTypes = d.dataTypes; // only for easier validation in the console d.dataTypes = needle ? needle.replacement : d.dataTypes; }) console.log(data); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM