使用lodash映射键或任何其他javascript方法(NodeJs)动态更改对象键

时间:2017-12-18 10:26:12

标签: javascript arrays json node.js object

这是我的json对象:

{ 
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' } 

}

我想在json上面这样做:

[{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}]

但有一个条件, 如果我的json头对象看起来像:

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

P.S:我正在更改firstName和lastName顺序

我想在json上面这样做:

[{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}]

查看名字和姓氏

怎么做?是否可以使用任何js方法执行此操作?

2 个答案:

答案 0 :(得分:3)

你可以通过执行一些简单的数组/对象操作来实现,

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

此处需要了解的事项Array#mapArray#reduceObject#keysArrow functions

注意:在处理对象时,我们不能依赖订单。这就是我依赖关键字符串的原因。

答案 1 :(得分:2)

您可以使用原生.map.reduce方法执行此操作:

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