The Methods of Array Iteration Illustration

Fahim Chowdhury
3 min readMay 7, 2021

Array.map()

Next way to iterate is map. it takes the item that’s from the array it dose something to it and then puts a new thing back in that same place in the array. now you can go to assign it to doubledn three that map that’s how can you go to iterate. you can return the item times two it’s just going to be double each item. that you can see at the example.

const numbers = [1, 2, 3];
const doubled = numbers.map(item => {
return item * 2;
});
console.log(doubled);

//Output is: [ 2, 4, 6 ]

Array.filter()

Array.filter() is take an array and it’s check each item in the array it gets again some kind of condition to see if it’s true or false. and each of these makes a brand new array so if you are going to pass in the numbers 1, 2, 3 it’s filter and with the condition. And for each of these items will be going to check condition. If that’s true and this is just a way to find out even numbers. now you can see the example.

const numbers = [1, -1, 2, 3];
const filtered = numbers.filter(item => {
return item >= 0;
});
console.log(filtered);

//Output is: [ 1, 2, 3 ]

Array.forEach()

If you are passing in the array forEach and it just does something forEach item in the array. So you can pass in the item and you pass in the index of the array and then you can see forEach in this example for each item you are just going to console log.

const array = [‘a’, ‘b’, ‘c’];
array.forEach((item, index) => {
console.log(item, index);
});
//Output is: a 0
//Output is: b 1
//Output is: c 2

Array.reduse()

You can take an array and you are going to to something and then pass the result to the next iteration along with the next item in the array. such as finding the sum of all the numbers in an array. let’s see the example.

const numbers = [1, -1, 2, 3];
const sum = numbers.reduce((result, item) => {
return result + item;
}, 0);

console.log(sum);

//Output is: 5

Array.some()

If you any item just check in the array. Suppose negative numbers sum means does any item in the entire array meet some condition is less theen zero it’s going to be true if no items meet the condition then it’s going to be false so you can will be see this is true. Because one of the items in the array is nagitive.

const numbers = [1, 2, 3, -1, 4];
const isNegativeNumber = numbers.some((item) => {
return item < 0;
});
console.log(isNegativeNumber);

//Output is: true

Array.every()

Now one of this every is kind of similar to some. But now every number has to meet the condition so if the all positive numbers. suppose any function you will be pass in each item if each more then zero then you’re put that true. If you make just one item negative when you run that will be output false.

const numbers = [1, 2, 3];
const allPositiveNumber = numbers.every((item) => {
return item > 0;
});
console.log(allPositiveNumber);

//Output is: true

Array.find()

Array.find() method will search inside of the array and it will return the first element for which the callback function returns the truthy value. If it dosen’t find one then it will return undefined. let’s see the example.

const names = [‘rocky’, ‘jony’, ‘sony’];
const result = names.find((item) => {
return item === ‘jony’;
});
console.log(result);

//Output is: jony

Array.findIndex()

Array.findIndex() is just like find everything in this code is the same except. But the only difference is that the find index method will find the element and it will return it’s index. It will be return the index of the item.

const names = [‘rocky’, ‘jony’, ‘sony’];
const result = names.findIndex((item) => {
return item === ‘sony’;
});
console.log(result);

//Output is: 2

String

JavaScript string is a primitive data type that is used to work with texts. We can be call any javaScript methods of the String object on a string primitive. Let’s see the example.

const friend1 = ‘Protiq’;
const friend2 = “Sadiq”;
const result = `My friends name ${friend1} and ${friend2}`;
console.log(result); //Output is: My friends name Protiq and Sadiq

Number

Numbers are the numeric data type. specifically it will be declare for integer or floating values. A Number object is created using the Number() constructor and pass in a number value. And all properties use the number methods are accessed from the object.

--

--