Array Operations with Javascript, Array Methods, Array Sorting and Array Iteration
Array : javascript array definition process
var colors= [ "Red", "White", "Green" ];
Array Metodları
toString () method: combines elements in the array with commas.
var colors= [ "Red","White","Green" ]; var combine = colors.toString(); output : Red,White,Green
join() method: combines the elements in the search with a desired character.
var colors = ["Red", "White", "Green"]; var combine = colors.join (";"); output: Red; White; Green
pop() method: Used to extract the last element from the array.
var colors = ["Red", "White", "Green"]; var subtract = colors.pop (); output: Red, White
push() method: used to add elements into the array.
var colors = ["Red", "White", "Green"]; var add = colors.push ("Blue"); output: Red, White, Green, Blue
shift() method: deletes the first element of the array.
var colors = ["Red", "White", "Green"]; colors.shift (); output: White, Green
unshift() method: adds to the beginning of the array.
var colors = ["Red", "White", "Green"]; var add = colors.unshift ("Blue"); output: Blue, Red, White, Green
splice() method: It consists of 3 parameters. The first parameter specifies the number of elements to start. the second parameter specifies how many elements to delete. the third element specifies the elments to be added.
var colors = ["Red", "White", "Green"]; colors.splice (2, 0, "Blue", "Black"); output: Red, White, Blue, Black, Green var colors = ["Red", "White", "Green"]; colors.splice (1, 1, "Blue", "Black"); output: Red, Blue, Black, Green
concat() method: Used to join 2 arrays.
var colors = ["Red", "White", "Green"]; var colors2 = ["Blue", "Black"]; var colorscombine = colors.concat (colors2); output: Red, White, Green, Blue, Black
slice() method: takes two parameters. the first parameter is the start parameter, the second parameter is the end parameter.
var colors = ["Red", "White", "Green", "Blue", "Black"]; var colors2 = colors.slice (1,3); output: White, Green
sort() metodu: array içinde sıralama yapar.
var colors = ["Red", "White", "Green", "Blue"]; colors.sort (); output: White, Red, Blue, Green
reverse() method: makes the array sort after.
var colors = ["Red", "White", "Green", "Blue"]; colors.sort (); colors.reverse (); output: Green, Blue, Red, White
to sort the numbers;
var numbers = [25, 31, 10, 61, 43, 55]; numbers .sort(function(x, y){return y - x}); output: 10,25,31,43,55,61 var numbers = [25, 31, 10, 61, 43, 55]; numbers .sort(function(x, y){return y - x}); numbers .reverse(); output: 61,55,43,31,25,10
foreach() method: Used to return within array. It takes 3 parameters 1. parameter value 2. parameter index 3. parameter array itself.
var numbers = [85, 42, 34, 47, 98]; numbers.forEach(arrayFunction); var output = ""; function arrayFunction(value) { output = output + value + ","; } output: 85,42,34,47,98,
var numbers = [85, 42, 34, 47, 98]; numbers.forEach(arrayFunction); var output = ""; function arrayFunction(value, index, array) { output = output + index+ ":"+ value + "<br>"; } output: 1:85 2:42 3:34 4:47 5:98
map() method: Used to return within array. It takes 3 parameters 1. parameter value 2. parameter index 3. parameter array itself. The difference from the foreach method creates a new array on the array. replacing the original series
var numbers = [85, 42, 34, 47, 98]; numbers.map(arrayFunction); var output = ""; function arrayFunction(value) { output = output + value + ","; } output: 85,42,34,47,98,
var numbers = [85, 42, 34, 47, 98]; numbers.map(arrayFunction); var output = ""; function arrayFunction(value, index, array) { output = output + index+ ":"+ value + "<br>"; } output: 1:85 2:42 3:34 4:47 5:98
filter() method: creates a new array from the return value used for filtering within the array.
var numbers = [85, 42, 34, 47, 98]; var filtered = numbers.filter(arrayFunction); // can be used both ways function arrayFunction(value) { return value > 45; } function arrayFunction(value,index,array) { return value > 45; } output: 85,47,98
reduce() method: can be used to add elements in the array.
var numbers = [85, 42, 34, 47, 98]; var toplam= numbers .reduce(arrayFunction); // can be used both ways function arrayFunction(total, value, index, array) { return total + value; } function arrayFunction(total, value) { return total + value; } output: 306
reduceRight() method: Unlike the reduce method, it goes from right to left in the array.
var numbers = [85, 42, 34, 47, 98]; var total= numbers .reduceRight(arrayFunction); // can be used both ways function arrayFunction(total, value, index, array) { return total + value; } function arrayFunction(total, value) { return total + value; } output: 306
every() method: checks if the condition is met for each array.
var numbers = [85, 42, 34, 47, 98]; var durum= numbers .every(arrayFunction); // can be used both ways function arrayFunction(value, index, array) { return value > 45; } function arrayFunction(value) { return value > 45; } output: false
some() method: checks whether there are any elements in the array elements that satisfy the condition.
var numbers = [85, 42, 34, 47, 98]; var durum= numbers .some(arrayFunction); // can be used both ways function arrayFunction(value, index, array) { return value > 45; } function arrayFunction(value) { return value > 45; } output: true
indexOf() and lastIndexOf() method: Search within array elements can take two parameters. 1. the element to search for 2. the element to begin with. The difference of the lastIndexOf () method starts searching from the end of the array. Returns -1 if the searched element is not found.
var colors = ["Red", "White", "Green", "Blue"]; var result = colors.indexOf ("White"); var result2 = colors.lastIndexOf ("White"); output: 2
find() and findIndex() method: The find method returns the value of the first element it finds. The findIndex method returns the index number of the first element it finds.
var numbers = [85, 42, 34, 47, 98]; var firstvalue= numbers.find(arrayFunction); var firstindex= numbers.findIndex(arrayFunction); function arrayFunction(value, index, array) { return value < 45; } output: 42 output: 2