Change key name in JavaScript object

We'll write a little function to fix the key the way you want.

function fix_key(key) { return key.replace(/^element_/, ''); }

Underscore

_.object(
  _.map(_.keys(json), fix_key),
  _.values(json)
)

ES5/loop

var keys = Object.keys(json);
var result = {};

for (i = 0; i < keys.length; i++) {
  var key = keys[i];
  result[fix_key(key)] = json[key];
}

return result;

ES5/reduce

Object.keys(json) . reduce(function(result, key) {
  result[fix_key(key)] = json[key];
  return result;
}, {});

ES6

Object.assign(
  {},
  ...Object.keys(json) .
    map(key => ({[fix_key(key)]: json[key]}))
)

This makes an array of little objects each with one key-value pair, using the ES6 "computed property name" feature, then passes them to Object.assign using the ES6 "spread" operator, which merges them together.


The problem is that you are looking for the property in the original object using the new key. Use keys[j] instead of key:

var keys = Object.keys(json);
for (var j=0; j < keys.length; j++) {
   var key = keys[j].replace(/^element_/, "");
   tmp[key] = json[keys[j]];
}

I uses a regular expression in the replace so that ^ can match the beginning of the string. That way it only replaces the string when it is a prefix, and doesn't turn for example noexample_data into no_data.

Note: What you have is not "a json", it's a JavaScript object. JSON is a text format for representing data.

Is that due to the fact that I converted the keys (Objects) to Strings (with the replace method)?

No. The keys are strings, not objects.


You could also change the properties in the original object by deleting the old ones and adding new:

var keys = Object.keys(json);
for (var j=0; j < keys.length; j++) {
   if (keys[j].indexOf("element_") == 0) {
     json[keys[j].substr(8)] = json[keys[j]];
     delete json[keys[j]];
   }
}

Tags:

Javascript