I am checking the attributes in a JavaScript object, replacing some of the keys by deleting the prefix \"element\" and saving the new values in another object.
We'll write a little function to fix the key the way you want.
function fix_key(key) { return key.replace(/^element_/, ''); }
_.object(
_.map(_.keys(json), fix_key),
_.values(json)
)
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;
Object.keys(json) . reduce(function(result, key) {
result[fix_key(key)] = json[key];
return result;
}, {});
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.