0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

great solution ! But it takes some time to compute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.