Skip to content
Advertisement

Create new Js Array of Object with new key name from old objects

I am trying to create a new Array of Object from the data which is another array of objects coming from API. The new Array which we will get in the end should get value from dataFromApi and updated key names to will be selected from another array that holds the previous key and new key name | label for those properties. This data will be exported in CSV file. So we are using reusable components for this export, I want to make this conversion of data dynamic. As the current key name looks like currentApplyStart and in the CSV file they are not good headers.

So,

We are getting a new key names array from this function. What this function is trying to solve is, get the pair of oldKeyName and its newKeyName || new key and label

JavaScript

which return something like this. This is demo data.

JavaScript
JavaScript

So let’s say we have an array of the object which comes from API

JavaScript

The data which i want it in the end should be like this

JavaScript

Observe is data, that I changed the old key name into a new key which I want to be header in csvFile. I can not mutate the original data , for that reason i have to create a new data array . The solution i tried was this but I was not getting the desired result.

JavaScript

Advertisement

Answer

Based on the later explanation, I’ve updated the answer.

Here’s the JSFiddle link: https://jsfiddle.net/_ghost/py8et9vf/57/

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement