JavaScript Array Functions

I mentioned creating arrays and some methods used in arrays

in the article titled Creating Arrays. In this article, how the methods that operate on arrays are used is explained with examples.

concat() Adds the called array to the array or values ​​sent to the method and returns the array.

every() Tests all elements in the array with the given function. It checks all elements in order until it finds the return value false. If all returned values ​​are true, the result returns true.

filter() Creates a new array from all the elements passed by the function passed to it.

forEach() Runs the specified function for each element of the array.

indexOf() Searches the array with the parameter sent to it and returns the first index it finds. If the searched value is not present in the array, it returns -1. join() Joins all values ​​in the array according to the specified glue.

lastIndexOf() Checks whether the specified element is at the end of the array. If it cannot find it at the end of the array, it returns -1.

map() Passes all the elements in the array through the specified function and creates a new array.

pop() Discards the last value in the array from the array.

push() Adds the specified elements to the end of the array.

reverse() Reverses the array. shift() Pulls the first element at the beginning of the array.

sort() Sorts the array.

some() checks whether SOME element passes the test with the specified function.

unshift() Adds values ​​to the beginning of the array.

toString() Converts the string into a textual expression.

JavaScript Array Method Examples


concat()

//concat adds the array in which the method is called to the array or values ​​sent to the method and returns the array.

// var newarray = oldarray.concat(value1[, value2[, ...[, valueN]]])

var fruits = ["Apple", "Banana"];

var vegetables = ["Broccoli", "Cabbage"];

var plants=[];

plants= vegetables.concat(fruits);

document.write(plants); //screen output: Broccoli, Cabbage, Apple, Banana

every()

//every tests all elements in the array with the given function.

//Checks all elements in order until the return value is found to be false. If all returned values ​​are true, the result returns true.

function ageBuyukMu(element, index, array) {

return element >= 18;

}

var status1= [20, 5, 8, 130, 44].every(ageBuyukMu); //false

var status2= [18, 54, 18, 130, 44].every(ageBuyukMu); //true

//every tests all elements in the array with the given function.

//Checks all elements in order until the return value is found to be false. If all returned values ​​are true, the result returns true.
function numberControl(element, index, array) {

//Information: NaN ->is a value produced if it cannot be converted to a number. '1' -> 1 while 'a' - > There is no number, it is NaN.

if(isNaN(element))

return false;

else

return true;

}

var case1= [5,"aaa",7,3].every(numberControl); //false

var case2= [18, 54, 18,152].every(numberControl); //true

console.log(status1);

console.log(status2);

filter()

//filter method creates a new array from all the elements passed by the function sent to it.

function bigNumbers(value) {

return value >= 25;

}

var new_array = [12, 5, 8, 130, 44].filter(largeNumbers);

// new_array value: [130, 44]

document.write(new_dizi);

forEach()

// forEach() method runs the specified function for each element of the array.

function exampleFunk(value) {

//multiplies each element of the array by 5 and writes it to the screen between h3 tags.

document.write("

"+value*5+"
");
}

[34,11,02,4].filter(exampleFunk);

indexOf()

//indexOf() method searches the array with the parameter sent to it and returns the first index it finds. If the searched value is not present in the array, it returns -1.

var array = [2, 9, 9];
array.indexOf(2); //0
array.indexOf(7); // -one
array.indexOf(9, 2); // 2
array.indexOf(2, -1); // -one
array.indexOf(2, -3); //0

join()

//join() method joins all values ​​in the array according to the specified glue.

var fruits = ["Apple", "Banana","Grapes","Orange"];

var array1 = fruits.join(","); //Apple,Banana,Grape,Orange

var array2 =fruits.join("+"); //Apple+Banana+Grape+Orange

lastIndexOf()

//lastIndexOf() method checks whether the specified element is at the end of the array. If it cannot find it at the end of the array, it returns -1.

// In addition to the searched value, a second optional value can be given. If it is positive, how many values ​​will be searched from the beginning of the array,

//negative indicates how many values ​​will be excluded from the search at the end of the array. Remember, even if it is not added to the search, it will be searched from the beginning of the directory.

//If it does not find a value, it will return -1.

var array = [2, 5, 9, 2];
array.lastIndexOf(2); //3
array.lastIndexOf(7); // -one
array.lastIndexOf(9, 1); // -one
array.lastIndexOf(9, 2); // 2
array.lastIndexOf(9, -3); // -one
array.lastIndexOf(9, -1); // 2

map()

//map() method specify all elements in the arrayPasses it through the function and creates a new array.

var numbers = [1, 4, 9];

var root = numbers.map(Math.sqrt);

// coke is now [1, 2, 3], the array of numbers is still [1, 4, 9]

var objectArray = [{key:1, value:10}, {key:2, value:20}, {key:3, value: 30}];

var newArray = kvArray.map(function(in){

var yArray = {};

yArray[incoming.key] = incoming.value;

return newArray;

});

// newSequence : [{1:10}, {2:20}, {3:30}],

// objectArray is still [{key:1, value:10}, {key:2, value:20}, {key:3, value: 30}]

pop()

//pop() method throws the last value in the array from the array.

var fruits = ["Apple", "Banana","Grapes","Orange"];

var son_meyve=mevveler.pop();

//fruits in new form ["Apple", "Banana", "Grape"]

//son_meyve : "Orange"

push()

// push() method adds the specified elements to the end of the array.

var fruits = ["Apple", "Banana"];

fruits.push("Grapes","Oranges");

//fruits new version: ["Apple", "Banana","Grape","Orange"];

reverse()

//reverse() method reverses the array.

var fruits = ["Apple", "Banana","Grapes","Orange"];

fruits.reverse();

//new version of fruits: ["Orange","Grape","Banana","Apple"]

shift()

//shift() method pulls the first element at the beginning of the array.

var fruits = ["Apple", "Banana","Grapes","Orange"];

var first_element=fruits.shift();

//fruits new version [ "Banana","Grapes","Orange"]

//first_eleman : "Apple"

shorts()

//sort() method sorts the array.

var fruits = ["Apple", "Banana","Quince","Orange"];

fruits.sort();

//fruits in new form ["Quince", "Apple", "Banana", "Orange"]

some()

//some() method checks whether SOME elements pass the test with the specified function.

function characterControl1(element, index, array){

//Checks if there is anyone larger than 5 frames. true if any

return element.length>5;

}

function characterControl2(element, index, array){

//Checks if there is anything larger than 10 frames. true if any

return element.length>10;

}

var fruits = ["Apple", "Banana","Quince","Orange"];

fruits.some(characterControl1); //true orange It will return true because it is larger than 5 characters.

fruits.some(characterControl2); //false The result will return false since there is no value larger than 10 characters.

unshift()

//unshift() method adds value to the beginning of the array.

var fruits = ["Apple", "Banana","Grapes","Orange"];

fruits.unshift("Quince");

//new version of the fruit series:["Quince","Apple","Banana","Grape","Orange"]

toString()

//toString() method converts the string into a text expression.

var fruits = ["Apple", "Banana","Quince","Orange"];

var text=fruits.toString();

//text variable: Apple, Banana, Quince, Orange