Page 118 - JavaScript
P. 118

firstName: "Kaylee",
             lastName: "Frye"
           }, {
             id: 3,
             firstName: "Jayne",
             lastName: "Cobb"
           }
         ];

         // Returns a new array of objects made up of full names.
         var reformatPersons = function(persons) {
           return persons.map(function(person) {
             // create a new object to store full name.
             var newObj = {};
             newObj["fullName"] = person.firstName + " " + person.lastName;

             // return our new object.
             return newObj;
           });
         };


        We can now call reformatPersons(personsArray) and received a new array of just the full names of
        each person.


         var fullNameArray = reformatPersons(personsArray);
         console.log(fullNameArray);
         /// Output
         [
           { fullName: "Malcom Reynolds" },
           { fullName: "Kaylee Frye" },
           { fullName: "Jayne Cobb" }
         ]


        personsArray and its contents remains unchanged.


         console.log(personsArray);
         /// Output
         [
           {
             firstName: "Malcom",
             id: 1,
             lastName: "Reynolds"
           }, {
             firstName: "Kaylee",
             id: 2,
             lastName: "Frye"
           }, {
             firstName: "Jayne",
             id: 3,
             lastName: "Cobb"
           }
         ]


        Merge two array as key value pair


        When we have two separate array and we want to make key value pair from that two array, we




        https://riptutorial.com/                                                                               75
   113   114   115   116   117   118   119   120   121   122   123