2

假设我们有 2 个 Javascript 对象和一个数组:

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

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

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

我想根据displayNames指示的映射来更改rawData中的字段名称,最终结果应该是:

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

谢谢!

4

2 回答 2

1

迭代rawData以构建新data对象并displayNames用作新属性名称的查找表:

var data = {};
for (var p in rawData)
    data[displayNames[p]] = rawData[p];
于 2013-04-09T18:32:34.440 回答
0
for (var oldKey in displayNames)
{
    var newKey = displayNames[oldKey];
    rawData[newKey] = rawData[oldKey];
    delete rawData[oldKey];
}
于 2013-04-09T18:33:35.540 回答