Home > Software design >  JS alter JSON value to match key
JS alter JSON value to match key

Time:03-12

I have the following JSON and I am trying to alter it to have the same key and value string:

var icons = {
      "123": 63103,
      "alarm-fill": 61697,
      "alarm": 61698
}

I'm trying the following code:

var newIcons = [];

for (var key in icons) {
    if (icons.hasOwnProperty(key)) {
        newIcons.key = key;
    }
}

console.log(newIcons);

It should return the following:

{
  "123": "123",
  "alarm-fill": "alarm-fill",
  "alarm": "alarm"
}

CodePudding user response:

var newIcons = {};

for (var key in icons) {
    newIcons[key] = key;
}

console.log(newIcons);
  • Related