最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to rename key most efficiently in array of items in javascript? - Stack Overflow

programmeradmin1浏览0评论

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 bees:

{"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".

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 bees:

{"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".

Share Improve this question edited Nov 9, 2015 at 2:56 Rayon 36.6k5 gold badges53 silver badges78 bronze badges asked Nov 9, 2015 at 2:55 RolandoRolando 62.8k104 gold badges281 silver badges423 bronze badges 4
  • Possible duplicate of JavaScript: Object Rename Key – Andrew Mast Commented Nov 9, 2015 at 3:23
  • Why can't you use these names in the first place? – Frederik.L Commented Nov 9, 2015 at 3:55
  • What patibility 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 Commented Nov 9, 2015 at 4:06
  • For example, you can increase responsiveness by doing this work asynchronously if the context allows for it. – allonhadaya Commented Nov 9, 2015 at 4:13
Add a ment  | 

5 Answers 5

Reset to default 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.

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.

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;
}

From https://stackoverflow./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')

arr.map(function (item) { return { num: item.id, fullname: item.name } });
发布评论

评论列表(0)

  1. 暂无评论