Home > Software design >  Convert keys in array to upper case
Convert keys in array to upper case

Time:02-16

I'm in need where all the keys in an array should be converted to uppercase. User will upload a excel all the worksheets are read into arrays separately eg: check details, ingredients, menu and desserts are different worksheets in excel.

Current JSON:

[
  [
    "filename",
    "details.xlsx"
  ],
  [
    "Check Details",
    [
      {
        "Outdated Label": "Outdated Label1",
        "Outdated Value": "Outdated Value"
      },
      {
        "Outdated Label": "Outdated Label2",
        "Outdated Value": "Outdated Value2"
      },
      {
        "Outdated Label": "Outdated Label3",
        "Outdated Value": "Outdated Value3"
      },
    {
        "Outdated Label": "Outdated Label4",
        "Outdated Value": "Outdated Value4"
      }
    ]
  ],
  [
    "ingredient",
    [
      {
        "ingredient Id": "1",
        "ingredient Code": "IG01",
        "ingredient Label": "Recipe 1"
      },
      {
        "ingredient Id": "2",
        "ingredient Code": "IG02",
        "ingredient Label": "Secret Recipe"
      },
      {
        "ingredient Id": "4",
        "ingredient Code": "IG04",
        "ingredient Label": "Chef special"
      },
      {
        "ingredient Id": "3",
        "ingredient Code": "IG03",
        "ingredient Label": "Today special"
      }
    ]
  ],
  [
    "menu",
    [
      {
        "Item Menu Id": "M01",
        "Item": "Pizza"
      },
      {
        "Item Menu Id": "M02",
        "Item": "Hotdogs"
      },
       {
        "Item Menu Id": "M03",
        "Item": "French Fries"
      }
    ]
  ],
  [
    "Desserts",
    [
      {
        "Menu Item Id": "DRT01",
        "Item": "Roasted strawberry crumble"
      },
       {
        "Menu Item Id": "DRT01",
        "Item": "Apple and butterscotch pie"
      }
    ]
  ]
  ]

Expected JSON :

[
  [
    "filename",
    "details.xlsx"
  ],
  [
    "Check Details",
    [
      {
        "OUTDATED LABEL": "Outdated Label1",
        "OUTDATED VALUE": "Outdated Value"
      },
      {
        "OUTDATED LABEL": "Outdated Label2",
        "OUTDATED VALUE": "Outdated Value2"
      },
      {
        "OUTDATED LABEL": "Outdated Label3",
        "OUTDATED VALUE": "Outdated Value3"
      },
    {
        "OUTDATED LABEL": "Outdated Label4",
        "OUTDATED VALUE": "Outdated Value4"
      }
    ]
  ],
  [
    "ingredient",
    [
      {
        "INGREDIENT ID": "1",
        "INGREDIENT CODE": "IG01",
        "INGREDIENT LABEL": "Recipe 1"
      },
      {
        "INGREDIENT ID": "2",
        "INGREDIENT CODE": "IG02",
        "INGREDIENT LABEL": "Secret Recipe"
      },
      {
        "INGREDIENT ID": "4",
        "INGREDIENT CODE": "IG04",
        "INGREDIENT LABEL": "Chef special"
      },
      {
        "INGREDIENT ID": "3",
        "INGREDIENT CODE": "IG03",
        "INGREDIENT LABEL": "Today special"
      }
    ]
  ],
  [
    "menu",
    [
      {
        "ITEM MENU ID": "M01",
        "ITEM": "Pizza"
      },
      {
        "ITEM MENU ID": "M02",
        "ITEM": "Hotdogs"
      },
       {
        "ITEM MENU ID": "M03",
        "ITEM": "French Fries"
      }
    ]
  ],
  [
    "Desserts",
    [
      {
        "MENU ITEM ID": "DRT01",
        "ITEM": "Roasted strawberry crumble"
      },
       {
        "MENU ITEM ID": "DRT01",
        "ITEM": "Apple and butterscotch pie"
      }
    ]
  ]
  ]

Note: All the keys are dynamic as it is uploaded from excel it could be anything.

CodePudding user response:

What you want is actually rename specific keys, here are some posts that talk about that :

JavaScript: Object Rename Key

Changing the key name in an array of objects?

How to rename properties of objects in array in javascript?

CodePudding user response:

This solution will mutate the keys of any object within the array, in place and recursively.

  in = "your JSON input"

  dataObject: any[] = [];

  out = '';

  ngOnInit(): void {
    this.dataObject = JSON.parse(this.in);
    this.searchArray(this.dataObject);
    this.out = JSON.stringify(this.dataObject);
  }

  searchArray(arr: any[]) {
    for (const entry of arr) {
      if (entry instanceof Array) this.searchArray(entry);
      else if (entry instanceof Object) this.keysToUpper(entry);
    }
  }

  keysToUpper(obj: any) {
    for (const key of Object.keys(obj)) {
      Object.assign(obj, { [key.toUpperCase()]: obj[key] });
      delete obj[key];
    }
  }

Stackblitz: https://stackblitz.com/edit/angular-ivy-zrjp5k?file=src/app/app.component.ts

Note: this assumes there are no arrays or other objects nested within another object.

CodePudding user response:

You can use recursion to do nested loop and remap the keys like this:

var orignalArr = [ [ "filename", "details.xlsx" ], [ "Check Details", [ { "Outdated Label": "Outdated Label1", "Outdated Value": "Outdated Value" }, { "Outdated Label": "Outdated Label2", "Outdated Value": "Outdated Value2" }, { "Outdated Label": "Outdated Label3", "Outdated Value": "Outdated Value3" }, { "Outdated Label": "Outdated Label4", "Outdated Value": "Outdated Value4" } ] ], [ "ingredient", [ { "ingredient Id": "1", "ingredient Code": "IG01", "ingredient Label": "Recipe 1" }, { "ingredient Id": "2", "ingredient Code": "IG02", "ingredient Label": "Secret Recipe" }, { "ingredient Id": "4", "ingredient Code": "IG04", "ingredient Label": "Chef special" }, { "ingredient Id": "3", "ingredient Code": "IG03", "ingredient Label": "Today special" } ] ], [ "menu", [ { "Item Menu Id": "M01", "Item": "Pizza" }, { "Item Menu Id": "M02", "Item": "Hotdogs" }, { "Item Menu Id": "M03", "Item": "French Fries" } ] ], [ "Desserts", [ { "Menu Item Id": "DRT01", "Item": "Roasted strawberry crumble" }, { "Menu Item Id": "DRT01", "Item": "Apple and butterscotch pie" } ] ] ]
var deepLoop = function(inputArray){

    /* Re-map the input array, input array will change based on recusrion call */
    inputArray.map(function(item){

        /* If current iteration is array, call the recusrion unless we reach an object */
        if(Array.isArray(item)){
            deepLoop(item);
        }
        else if(typeof item === "object"){
            /* This is object, update the keys */
            Object.keys(item).forEach(function(key){
                /* Rename key, solution from => https://stackoverflow.com/questions/4647817/javascript-object-rename-key */
                Object.defineProperty(item, key.toUpperCase(),
                Object.getOwnPropertyDescriptor(item, key));
                delete item[key];
                
            });
            
        }
        return item;
    });
};

/* First Call */
deepLoop(orignalArr);

console.log("Updated Array => ", orignalArr);

  • Related