3

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!

0

2 Answers 2

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]; 
Sign up to request clarification or add additional context in comments.

1 Comment

You're creating a global variable p.
1
for (var oldKey in displayNames) { var newKey = displayNames[oldKey]; rawData[newKey] = rawData[oldKey]; delete rawData[oldKey]; } 

1 Comment

This will only work if displayNames does not contain a key that is already in rawData. Although the OP asked to change the fieldnames of rawData, it's safer to create a new object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.