0

I have a JSON object array where I need to rename the keys based on values in the first object. Trying to do this in NodeJS but not having any luck.

I could probably brute force it with a couple of loops but was hoping for a more scalable solution since the number of "columns" change from time to time.

Here is an example

[{"A" : "Key1", "B" : "Key2", "C" : "Key3"},
 {"A" : "Data1", "B" : "Data2", "C" : "Data3"},
 {"A" : "Data5", "B" : "Data5", "C" : "Data7"}]

I would like the result to be like

[{"Key1" : "Key1", "Key1" : "Key2", "Key1" : "Key3"},
 {"Key1" : "Data1", "Key2" : "Data2", "Key3" : "Data3"},
 {"Key1" : "Data5", "Key2" : "Data5", "Key3" : "Data7"}]

3 Answers 3

1
let arr = [{"A" : "Key1", "B" : "Key2", "C" : "Key3"},
{"A" : "Data1", "B" : "Data2", "C" : "Data3"},
{"A" : "Data5", "B" : "Data5", "C" : "Data7"}];

const keys = Object.keys(arr[0]).map(i => arr[0][i]);
let result = arr.map(obj => {
    const replacedObj = {};
    const oriKeys = Object.keys(obj);
    for (let i = 0; i < keys.length; i++) {
        replacedObj[keys[i]] = obj[oriKeys[i]]
    };
    return replacedObj;
});
console.log(JSON.stringify(result));
Sign up to request clarification or add additional context in comments.

2 Comments

Wow that was quick and exactly what I needed thank you. Now I need to study this and understand what you did
It's a little flaky in the sense that you will get incorrect results if the keys are not defined/returned in the same order (the order of object properties is not guaranteed in JavaScript/JSON).
0

Using Object.entries() with some creative mapping, reducing, destructuring and spreading:

o = i.map(x => Object.entries(x).reduce((a, [k, v]) => ({...a, [i[0][k]]: v}), {}));

Complete snippet:

let input, output;

input = [
  {"A" : "Key1", "B" : "Key2", "C" : "Key3"},
  {"A" : "Data1", "B" : "Data2", "C" : "Data3"},
  {"A" : "Data5", "B" : "Data5", "C" : "Data7"}
];

output = input.map(x => Object.entries(x).reduce((a, [k, v]) => ({...a, [input[0][k]]: v}), {}));

console.log(output);

Comments

0

Lets say the old array is stored in a var called oldArray:

var keys = Object.keys(oldArray[0]); // get first object keys
var newArray = oldArray.map(function(obj,index){
// Iterate over each object to replace keys
if(index === 0) return obj; /* if first object we dont need to replace 
keys */
var objKeys = Object.keys(obj); //old keys for reference only
  return Object assign({},{
      [keys[0]]: obj[objKeys[0],  // assigning first object keys with 
       current 
      [keys[1]]: obj[objKeys[1],  // object values
      [keys[2]]: obj[objKeys[3],
 });
}); 
console.log(newArray);

/* You also can change the key value asignation with a for, so you 
can handle not only 3 key values object, this could be optimized 
with es6 ans spread operator definition but rather to implement it in 
es5 for less complexity */

1 Comment

Same comment as for dewfall's answer. Incorrect results based on the order in which the keys are returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.