0

I have this array that I get from the serve:

dataFromServer = [
 {
  created_date: "02/10/2019"
  date_of_birth: "01/01/2000"
  email: "test@test.com"
  first_name: "test"
  last_name: "test"
  mobile_phone: "999-999-9999"
  registration_id: "3344"
 },
 {
  created_date: "02/10/2015"
  date_of_birth: "01/01/1980"
  email: "test2@test2.com"
  first_name: "test2"
  last_name: "test2"
  mobile_phone: "111-222-333"
  registration_id: "123"
 }
]

and I have to put it in another array to get rid of the "_" in between each property. So this is what I'm doing:

      const newArray = []
      dataFromServer.foreach(obj => {
      newArray.push(
        {
          lastName: obj.last_name,
          firstName: obj.first_name,
          dateOfBirth: obj.date_of_birth,
          registrationId: obj.registration_id,
          createdDate: obj.created_date,
          email: obj.email,
          mobile_phone: obj.mobile_phone
        });
    });

Is there a better/ clear way in pure javascript (maybe using destructuring) or using Lodash? Thank a lot!

Devmix
  • 1,599
  • 5
  • 36
  • 73
  • 2
    Look into [`map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). – Heretic Monkey Feb 28 '19 at 14:14
  • Sorry I can't reply cause of the false duplicated status. You can try this const newArray = dataFromServer.map(obj => { var conv = {}; Object.keys(obj).forEach(k => { var newKey = k.split('_').map( (e,i) => { if ( i === 0 ) return e; return e.charAt(0).toUpperCase() + e.slice(1) }).join(''); conv[newKey] = obj[e]; }); return conv; }); – farvilain Feb 28 '19 at 14:17
  • 2
    @farvilain - It's not false. See the answers to the linked question. – T.J. Crowder Feb 28 '19 at 14:20
  • I've read it, and it's not working cause it's a similar question, not a duplicate one, like '==' doesnt' mean '===' – farvilain Feb 28 '19 at 14:29
  • 1
    @farvilain - It does on SO. If the other question's answers answer this question (and they do), that's SO's definition of "duplicate." – T.J. Crowder Feb 28 '19 at 14:34

2 Answers2

0

Yes, you can use map, perhaps (with ES2015+) with an arrow function:

const newArray = dataFromServer.map(obj => ({
    lastName: obj.last_name,
    firstName: obj.first_name,
    dateOfBirth: obj.date_of_birth,
    registrationId: obj.registration_id,
    createdDate: obj.created_date,
    email: obj.email,
    mobile_phone: obj.mobile_phone
}));

Live Example:

const dataFromServer = [
 {
  created_date: "02/10/2019",
  date_of_birth: "01/01/2000",
  email: "test@test.com",
  first_name: "test",
  last_name: "test",
  mobile_phone: "999-999-9999",
  registration_id: "3344"
 },
 {
  created_date: "02/10/2015",
  date_of_birth: "01/01/1980",
  email: "test2@test2.com",
  first_name: "test2",
  last_name: "test2",
  mobile_phone: "111-222-333",
  registration_id: "123"
 }
];

const newArray = dataFromServer.map(obj => ({
    lastName: obj.last_name,
    firstName: obj.first_name,
    dateOfBirth: obj.date_of_birth,
    registrationId: obj.registration_id,
    createdDate: obj.created_date,
    email: obj.email,
    mobile_phone: obj.mobile_phone
}));
console.log(newArray);
.as-console-wrapper {
  max-height: 100% !important;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-2
const newArray = dataFromServer.map(obj => ({
      lastName: obj.last_name,
      firstName: obj.first_name,
      dateOfBirth: obj.date_of_birth,
      registrationId: obj.registration_id,
      createdDate: obj.created_date,
      email: obj.email,
      mobile_phone: obj.mobile_phone
    }));
TKoL
  • 13,158
  • 3
  • 39
  • 73