简体   繁体   中英

Dynamically changing object keys with 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 make above json like this:

[{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 if my json header objects looks like:

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

PS: I am changing firstName and lastName order

I want to make above json like this:

[{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 the firstname and lastname

How to do this? is it possible to do this with any js method?

You can do it with performing some simple array/object manipulations,

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

Things to learn here Array#map , Array#reduce , Object#keys , Arrow functions .

Note: We cannot rely on orders when it comes to dealing an object. That's the reason why I have relied on the key string.

You can do it using native .map and .reduce methods:

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;
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM