【问题标题】:Convert keys in array to upper case将数组中的键转换为大写
【发布时间】:2022-02-15 18:37:45
【问题描述】:

我需要将数组中的所有键都转换为大写。用户将上传一个excel,所有工作表分别读入数组例如:检查细节,配料,菜单和甜点是excel中的不同工作表。

当前 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"
      }
    ]
  ]
  ]

预期的 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"
      }
    ]
  ]
  ]

注意:所有键都是动态的,因为它是从 excel 上传的,可以是任何东西。

【问题讨论】:

  • 到目前为止你尝试过什么?你被困在哪里了?
  • 另请注意,array(s) 没有 keys...objects 有...!!!

标签: javascript angular typescript


【解决方案1】:

你想要的实际上是重命名特定的键,这里有一些讨论这个的帖子:

JavaScript: Object Rename Key

Changing the key name in an array of objects?

How to rename properties of objects in array in javascript?

【讨论】:

    【解决方案2】:

    此解决方案将原地并递归地改变数组中任何对象的键。

      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

    注意:这假设没有数组或其他对象嵌套在另一个对象中。

    【讨论】:

      【解决方案3】:

      您可以使用递归来执行嵌套循环并重新映射键,如下所示:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-05
        • 2011-02-28
        • 1970-01-01
        • 2020-05-08
        • 2019-09-13
        • 1970-01-01
        相关资源
        最近更新 更多