Array.map vs Array.forEach

forEach can be used just like a traditional ‘for loop’ that iterates each of the array element once.

If you are working with JavaScript, then you must have come across these two similar looking array methods: Array.prototype.map() and Array.prototype.forEach().

Most of the developers thinks that Array.map function and forEach for an Array is the same, but it is not. Lets see some examples:
1. Array.prototype.forEach
forEach can be used just like a traditional ‘for loop’ that iterates each of the array element once.

let arrayElements = ['A','C','D'];  
arrayElements.forEach((ele)=>{  
  console.log(ele);  
 })  

The above example will generate the following –
A
C
D

2. Array.prototype.map
The map() function creates a new Array with the results of calling a function for every element of the calling array.

let arrayElements = [1, 4, 6];  
var result = arrayElements .map((ele)=>{  
  return (ele * 2);  
});  
console.log(result);  

The output of the code is:
[ 2, 8, 12 ]
In the above example we are iterating through each element of Array and multiplying it by 2.

Advertisement

Create an array in xslt

You can create any data type model you want to in xslt.

To create an array, just create a variable for it.

Example:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes"/>
 
    <xsl:variable name="array" as="element()*">
        <Item>A</Item>
        <Item>B</Item>
        <Item>C</Item>
    </xsl:variable>
 
    <xsl:template match="/">
        <xsl:value-of select="$array[2]"/>
    </xsl:template>
 
</xsl:stylesheet>

Output

B

%d bloggers like this: