Dynamically changing object keys using lodash map keys or any other javascript methods (NodeJs)

This is my json object:

{ excels: [ { field1: 'Mohamed', field2: '123456789', field3: 'Sameer', field4: 'Chennai', field5: 'Chennai', field6: 'Asia', field7: '11111', field8: '10-10-1990', field9: ' sameer@gmail.com '}, { field1: 'Ganesh', field2: '987654321', field3: 'Pandiyan', field4: 'Chennai', field5: 'Chennai', field6: 'Asia', field7: '11111', field8: '10-10-1990', field9: ' ganesh@gmail.com ' } ], header: { '1': 'firstName', '2': 'gsm', '3': 'lastName', '4': 'street', '5': 'city', '6': 'region', '7': 'postcode', '8': 'dob', '9': 'email' } } 

I want to do the above json as follows:

 [{firstName:Mohamed;gsm:123456789;lastName:Sameer;street:Chennai;city:Chennai;region:Asia;postcode:11111;dob:10-10-1990;email: sameer@gmail.com },{firstName:Ganesh;gsm:987654321;lastName:Pandiyan;street:Chennai;city:Chennai;region:Asia;postcode:11111;dob:10-10-1990;email: ganesh@gmail.com }] 

But one condition, in case my json header objects look like this:

 header: { '1': 'lastName', '2': 'gsm', '3': 'firstName', '4': 'street', '5': 'city', '6': 'region', '7': 'postcode', '8': 'dob', '9': 'email' } 

PS: I change firstName and lastName order

I want to do the above json as follows:

 [{firstName:Sameer;gsm:123456789;lastName:Mohamed;street:Chennai;city:Chennai;region:Asia;postcode:11111;dob:10-10-1990;email: sameer@gmail.com },{firstName:Pandiyan;gsm:987654321;lastName:Ganesh;street:Chennai;city:Chennai;region:Asia;postcode:11111;dob:10-10-1990;email: ganesh@gmail.com }] 

See first and last name

How to do it? can this be done using any js method?

+5
source share
2 answers

You can do this by doing some simple manipulations with arrays / objects,

 var result = source.excels.map((details) => Object.keys(details).reduce((a, b) => (a[source.header[b.replace('field', '')]] = details[b], a), {})); console.log(result); // result would contain [{firstName:'Mohamed',gsm:123456789, ...... 

Demo

Details here are Array#map , Array#reduce , Object#keys , Arrow functions .

Note. We cannot rely on orders when it comes to the facility. This is the reason I relied on the key line.

+3
source

You can do this using your own . map and . :

 const persons = { excels: [ { field1: 'Mohamed', field2: '123456789', field3: 'Sameer', field4: 'Chennai', field5: 'Chennai', field6: 'Asia', field7: '11111', field8: '10-10-1990', field9: ' sameer@gmail.com '}, { field1: 'Ganesh', field2: '987654321', field3: 'Pandiyan', field4: 'Chennai', field5: 'Chennai', field6: 'Asia', field7: '11111', field8: '10-10-1990', field9: ' ganesh@gmail.com ' } ], header: { '1': 'firstName', '2': 'gsm', '3': 'lastName', '4': 'street', '5': 'city', '6': 'region', '7': 'postcode', '8': 'dob', '9': 'email' } } const personKeys = Object.keys(persons.header) const formattedPersons = persons.excels.map((p) => { const formattedPerson = personKeys.reduce((newPerson, currentKey) => { newPerson[persons.header[currentKey]] = p[`field${currentKey}`] return newPerson }, {}) return formattedPerson; }) 
+2
source

Source: https://habr.com/ru/post/1274146/


All Articles