6

I have an array of 10000 objects. Each object is like this:

{"id":5, "name": "Jordin Sparks}

What is the most efficient and fastest way for me to rename keys such that every object in the array becomes:

{"num":5, "fullname": "Jordin Sparks"}

In other words, "id" property is renamed to "num" and the "name" property for each object is renamed to "fullname".

Rayon
  • 36,219
  • 4
  • 49
  • 76
Rolando
  • 58,640
  • 98
  • 266
  • 407
  • Possible duplicate of [JavaScript: Object Rename Key](http://stackoverflow.com/questions/4647817/javascript-object-rename-key) – Andrew Mast Nov 09 '15 at 03:23
  • Why can't you use these names in the first place? – Frederik.L Nov 09 '15 at 03:55
  • What compatibility are you looking for? Can any features be used to achieve this efficiency? Are the objects in question guaranteed to only have these keys? Space efficiency or time efficiency?... Efficiency is best understood in context. – allonhadaya Nov 09 '15 at 04:06
  • For example, you can increase responsiveness by doing this work asynchronously if the context allows for it. – allonhadaya Nov 09 '15 at 04:13

5 Answers5

2

Brute force approach. Convert to string, rename fields and parse to JSON

var src = {"id":5, "name": "Jordin Sparks"};

var str = JSON.stringify(src);

var rst = JSON.parse(str.replace(/"id"/g, '"num"').replace(/"name"/g, '"fullname"'));
console.debug(rst);

Edit: modify replace to global.

Jules
  • 1,423
  • 13
  • 22
  • This looks like it could be the fastest, but you'd need to run timings to be sure the naiive approach isnt faster – jamylak Nov 09 '15 at 05:33
1

I have no idea if this is indeed the most efficient, but here is my attempt.

for(var i = arr.length; i--; ){
  var obj = arr[i];
  obj.num = obj.id;
  obj.fullname = obj.name;
  delete obj.id;
  delete obj.name;
}
Shashank
  • 13,713
  • 5
  • 37
  • 63
1

Personally, I would do it like this.

function renameKeys(arr, nameMap) {
  // loop around our array of objects
  for(var i = 0; i < arr.length; i++) {
    var obj = arr[i];
    // loop around our name mappings
    for(var j = 0; j < nameMap.length; j++) {
       var thisMap = nameMap[j];
       if(obj.hasOwnProperty(thisMap.from)) {
         // found matching name
         obj[thisMap.to] = obj[thisMap.from];
         delete obj[thisMap.from];
       }
    }
  }
}

You would call it like so, where myArray is your array of objects.

renameKeys(myArray, [ 
  {from: "id", to: "num" }, 
  { from: "name", to: "fullname" } 
]);

Has the advantage that it is reusable for any number of name re-mappings. Doesn't modify native prototypes. And only iterates once around the array, no matter how many re-mappings take place.

Shaun
  • 933
  • 9
  • 16
0

From https://stackoverflow.com/a/4648411/5403473:

Object.prototype.renameProperty = function (oldName, newName) {
    // Do nothing if the names are the same
    if (oldName == newName) {
        return this;
    }

    // Check for the old property name to avoid a ReferenceError in strict mode.
    if (this.hasOwnProperty(oldName)) {
        this[newName] = this[oldName];
        delete this[oldName];
    }

    return this;
};

You would run objectOf1000.renameProperty('id', 'num') and objectOf1000.renameProperty('name', 'full name')

Community
  • 1
  • 1
Andrew Mast
  • 311
  • 1
  • 17
0
arr.map(function (item) { return { num: item.id, fullname: item.name } });
patrick
  • 9,290
  • 13
  • 61
  • 112