JavaScript array map

In this Tutorial we want to describe you a code that help you in
understanding JavaScript array map. For this we are using a JavaScript language as
script language. The function name functionmakeplural accept single as parameter
variable, the variable single accept the list of value and store in it. Finally the
document .write ( ) method print all the list of element in a singular form. The
for loop evaluate the length of the element in singular form and the document. write
print the length of element. The Single variable map the singular element to plural and
store in variable plural. The document.write print the element in plural form. The
for loop return you the number of times your script runs and the length of the
plural element in the list printed by the docment.write ( ) method.
Arraymap.html
<html>
<head>
<title>Array Map</title>
</head>
<body>
<script language="javascript" type="text/javascript">
function makePlural(single)
{
return single.replace(/o/g, "e");
}
var singles = ["Foot", "Goose", "Moose","Choose","Toose"];
document.write("<b>"+"Elements in singular form are: "+"</b>"+"<br>");
document.write("======================"+"<br>");
for(var i=0;i<singles.length;i++){
document.write(singles[i]+"<br>");
}
var plural = singles.map(makePlural);
document.write("<b>"+"Elements in Plural form are: "+"</b>"+"<br>");
document.write("======================"+"<br>");
for(var i=0;i<plural.length;i++){
document.write(plural[i]+"<br>");
}
</script>
</body>
</html>
|
Output of the program
Download Source code

|