Rajratna maitry
4 min readJan 6, 2020

Js Array methods must know

Arrays are one of the most common things that you’re going to use as a programmer so today I’m going to be covering JavaScript array methods that are going to make your life so much easier.

Get started

var items = [{   'name': 'TV',           'price': 148    },{   'name': 'Movie ticket', 'price': 40     },{   'name': 'Book',         'price': 100    },{   'name': 'Album',        'price': 80     }];

I just have an array of items that we’re going to use for all these different array methods

array.some(function(currentValue, index, arr), thisValue)

the first method that we’re going to talk about is the Some function which is a bit different than most of our other functions since instead of returning a brand-new array it’s actually going to return true or false so we can check if some of the items in this array have a price less than 100 so we can say in expensive items we’ll say has an expensive item since we want to see if this array has any inexpensive items and all we do is say items to dot some and this is going to take that same exact syntax as all these other array methods but it’s just going to check our return value and as soon as a single item returns true it’s going to return true for the entire thing so we can just say item dot price is less than or equal to 100 so if anything is less than or equal to 100 we’ll say that this array has inexpensive items in it and we can then log this as an expensive items and if you say that you see it says true because it does have items less than or equal to 100 you can kind of think of this as any it just checks the array to see if anything in the array returns true for this and if it does the entire thing returns true but let’s say we wanted to check if there’s any items that are completely free so less than or equal to zero and you’ll see it returns false because nothing in the array returns true for this statement

const hasInExpItems= items.some((item) => {
return item.price <=100
});
// output console.log(hasInExpItems);
true

array.every(function(currentValue, index, arr), thisValue)

the next array method every is very similar to some except for instead of checking for a beast one item it checks to make sure every single item falls under that so if we say less than or equal to 100 this is going to check if every item in the array is less than 100 and if you save that you see that we get false returned over here because there are items more than 100 if we change this to be a thousand though and ran it you’ll see that we get true because there are all the items in this array are less than 1000 so everything returns true for this so the entire thing is going to return true

const hasInExpItems= items.every((item) => {
return item.price <=1000
});
// output console.log(hasInExpItems);
true

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

the next method that I want to talk about the reduce method is a bit different than all of the other methods since it’s actually doing some operation on the array and returning a combination of all those different operations so if we wanted to get the total price of all the different items in this array normally what you would do is you would just do a for Loop and add the price of your single time and at the end of the for loop you would print out the price but you can use the reduce method to do this instead and the syntax for the reduced method is a bit different instead of taking an item it takes an item and a property for what we want to reduce everything into in our case this is just going to be the sub or the current total so this is going to be the total after each iteration of the array and then it also takes a second parameter which is going to be your starting point in our case we want to start our total at zero and then in here all we do is return the price of the item and we add it to whatever the current total is and now if we print out that total you’ll see that we get an error and that’s because this current total actually is going to be the first method in our parameter and the second method is the actual item that we’re going to be generating over and now if we say that you’ll see that we get the total to be 368

const total= items.reduce((currentIndex,item) => {
return item.price + currentIndex;
},0);
// output console.log(total);
368

No responses yet

Write a response