DEV Community

Cover image for Map() function with the help of JavScript Array of Objects....
swapnanilWebDeveloper
swapnanilWebDeveloper

Posted on

Map() function with the help of JavScript Array of Objects....

JavaScript Map function Regarding Array of objects...

var student = [
            { fname: "mayuk", lname: "mukherjee", science : 
   {'physics' : '99', 'chemisrty' : '88', 'mathematics' : '97'} },
            { fname: "suryendu", lname: "sarkar",science : 
   {'physics' : '90', 'chemisrty' : '68', 'mathematics' : '68'}},
            { fname: "sanlap", lname: "gadai",science : {'physics' 
   : '83', 'chemisrty' : '98', 'mathematics' : '67'}}
        ]; 
Enter fullscreen mode Exit fullscreen mode

In the above code we have just created a JavaScript array of objects, called student. In that Array of objects each Single element is an object and each object has three properties =>
a) fname , lname, science
b) science is itself a property and a object
c) "science" property will be called nested object
d) "science" object has three properties like -> 'physics', 'chemistry', 'mathematics'.

 var b = student.map(stu_name);
 var c = student.map(science_total);

        for(var i = 0; i < b.length; i++){
            document.write("Total marks for "+b[i]+" is : "+c[i]+"<br>");
        }

        function stu_name(name_data){
             var nm;
             nm = name_data.fname+" "+name_data.lname;
             return nm;
        }

        function science_total(marks){
            var sumation;
            sumation = parseInt(marks.science.physics) + parseInt(marks.science.chemisrty) + parseInt(marks.science.mathematics);
            return sumation;
        }   
Enter fullscreen mode Exit fullscreen mode

a) Now by map() function we will access each single object from this array of objects called "student".
b) We have created two map() function which are callback functions => stu_name(), science_total().

output : 
Total marks for mayuk mukherjee is : 284
Total marks for suryendu sarkar is : 226
Total marks for sanlap gadai is : 248
Enter fullscreen mode Exit fullscreen mode

a) after all the code you write you will get this above output

How you deploy your code on web server

Top comments (0)