Javascript Arrays for Absolute Beginner

Javascript Arrays Iteration Methods

forEach method iterate over an each array elements and call a callback function to get its values and index.

The map() method does not execute the function for array elements without values and does not change the original array. The map() method creates a new array by calling a function on each array element.

Below example, works perfectly with array.map() methods and array.forEach() methods respectively

<!DOCTYPE html>
<html lang=“en”>
<head>
<title>Document</title>
</head>
<body>
<ul id=“list”></ul>
  <script>
    let ul = document.getElementById(‘list’)
    let cookies = [“strawberry-cookies”, “Stick-sweets”, “Lollipop”, “Kolas”, “Pinky”]
    cookies.forEach(function(value){
	ul.innerHTML += `<li>${value}</li>`
      })
</script>
</body>
</html>

The array.filter() method filter our arrays according to the passed test and return a new array. also The array.some() and The array.every() method check if some array values pass a test.

Let Cookies = [“Strawberry-Cookies”, “Stick-Sweets”, “Lollipop”, “Kolas”, “Pinky”]
Cookies.Filter(MyFunction) 
Function MyFunction(Value) {
     If (Value == “Kolas”) {
         Ul.InnerHTML += <Li>${Value} *</Li>
      }Else {
         Ul.InnerHTML += <Li>${Value}</Li>
     }
 }

Array.indexOf() method returns the index/positions of element present in an array.

Leave a Comment

Your email address will not be published. Required fields are marked *