Home > Software design >  How to reformat this array of objects to an object of objects
How to reformat this array of objects to an object of objects

Time:08-06

can anyone help me out here I cant figure out how to reformat this array of objects to an object of objects so i want to turn

let allStringData = [
  {
    "String": "Alien Black Diamond 16",
    "Material": "Polyester",
    "Stiffness (lb/in)": "190.3",
    "Tension Loss (%)": "50.7",
    "Spin Potential": "4.5"
  },
  {
    "String": "Alpha Gut 2000 16",
    "Material": "Nylon",
    "Stiffness (lb/in)": "159.4",
    "Tension Loss (%)": "16.5",
    "Spin Potential": "3.1"
  }
]

To this

let allStringData = {
  "Alien Black Diamond 16" : {
    "String": "Alien Black Diamond 16",
    "Material": "Polyester",
    "Stiffness (lb/in)": "190.3",
    "Tension Loss (%)": "50.7",
    "Spin Potential": "4.5"
  },
  "Alpha Gut 2000 16" : {
    "String": "Alpha Gut 2000 16",
    "Material": "Nylon",
    "Stiffness (lb/in)": "159.4",
    "Tension Loss (%)": "16.5",
    "Spin Potential": "3.1"
  }
}

CodePudding user response:

figure it out

let finalObject = {}
allStringsData.forEach((item) => {
    finalObject[item["String"]] = item;
})

CodePudding user response:

let stringData = {};
allStringData.forEach(string => {
    stringData[string.String] = string;
});
  • Related