How to change a JavaScript object's field names? - Stack Overflow

Lets say we have 2 Javascript objects and an array:var rawData = {"dModel": "Deluxe"

Lets say we have 2 Javascript objects and an array:

var rawData = {
    "dModel": "Deluxe",
    "sizeMB": 256,
};

var displayNames = {
    "dModel": "Device Model",
    "sizeMB": "Size(MB)"
};

var fieldNames = ["dModel", "sizeMB"];

I want to change the field names in rawData according to the mapping indicated by displayNames, the final result should be:

var data = {
    "Device Model": "Deluxe",
    "Size(MB)": 256,
};

Thanks!

Lets say we have 2 Javascript objects and an array:

var rawData = {
    "dModel": "Deluxe",
    "sizeMB": 256,
};

var displayNames = {
    "dModel": "Device Model",
    "sizeMB": "Size(MB)"
};

var fieldNames = ["dModel", "sizeMB"];

I want to change the field names in rawData according to the mapping indicated by displayNames, the final result should be:

var data = {
    "Device Model": "Deluxe",
    "Size(MB)": 256,
};

Thanks!

Share Improve this question asked Apr 9, 2013 at 18:30 CGPCGP 3033 silver badges8 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 1

Iterate rawData to build the new data object and use displayNames as a lookup table for the new property names:

var data = {};
for (var p in rawData)
    data[displayNames[p]] = rawData[p];
for (var oldKey in displayNames)
{
    var newKey = displayNames[oldKey];
    rawData[newKey] = rawData[oldKey];
    delete rawData[oldKey];
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745263459a4619318.html

相关推荐

  • How to change a JavaScript object's field names? - Stack Overflow

    Lets say we have 2 Javascript objects and an array:var rawData = {"dModel": "Deluxe"

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信