结合javascript

时间:2018-07-08 22:08:48

标签: javascript arrays node.js

我在javascript中有一个对象数组。 示例:

objArr = [{"FirstName":"John","LastName":"Doe","Age":35},{"FirstName":"Jane","LastName":"Doe","Age":32}]

我想创建一个像这样的对象数组

newObjArr=[{"Name":"John Doe","Age":35},{"Name":"Jane Doe","Age":32}]

我应该怎么做?

1 个答案:

答案 0 :(得分:2)

您可以使用Array.map

  

map()方法使用调用a的结果创建一个新数组   在调用数组中的每个元素上提供了功能。

const objArr = [{"FirstName":"John","LastName":"Doe","Age":35},{"FirstName":"Jane","LastName":"Doe","Age":32}]


const newObjArr = objArr.map(({FirstName, LastName, Age}) => {
    return { 
      Name: `${FirstName} ${LastName}`,
      Age
    };
});

console.log(newObjArr);

或者一个丑陋的班轮:

objArr.map(({FirstName, LastName, Age}) => ({ Name: `${FirstName} ${LastName}`, Age }));