Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

methods of javascript array

[1, 2, 3].push(4); // [1, 2, 3, 4]
[1, 2, 3].pop(); // [1, 2]
[1, 2, 3].shift(); // [2, 3]
[1, 2, 3].unshift(0); // [0, 1, 2, 3]
['a', 'b'].concat('c'); // ['a', 'b', 'c']
['a', 'b', 'c'].join('-'); // a-b-c
['a', 'b', 'c'].slice(1); // ['b', 'c']
['a', 'b', 'c'].indexOf('b'); // 1
['a', 'b', 'c'].includes('c'); // true
[3, 5, 6, 8].find((n) => n % 2 === 0); // 6
[2, 4, 3, 5].findIndex((n) => n % 2 !== 0); // 2
[3, 4, 8, 6].map((n) => n * 2); // [6, 8, 16, 12]
[1, 4, 7, 8].filter((n) => n % 2 === 0); // [4, 8]
[2, 4, 3, 7].reduce((acc, cur) => acc + cur); // 16
[2, 3, 4, 5].every((x) => x < 6); // true
[3, 5, 6, 8].some((n) => n > 6); // true
[1, 2, 3, 4].reverse(); // [4, 3, 2, 1]
[3, 5, 7, 8].at(-2); // 7
Comment

javascript array functions

concat(arr1,[...]) // Joins two or more arrays, and returns a copy of the joined arrays
copyWithin(target,[start],[end]) // Copies array elements within the array, to and from specified positions
entries() // Returns a key/value pair Array Iteration Object
every(function(currentval,[index],[arr]),[thisVal]) // Checks if every element in an array pass a test
fill(val,[start],[end]) // Fill the elements in an array with a static value
filter(function(currentval,[index],[arr]),[thisVal]) //	Creates a new array with every element in an array that pass a test
find(function(currentval,[index],[arr]),[thisVal]) // Returns the value of the first element in an array that pass a test
findIndex(function(currentval,[index],[arr]),[thisVal]) // Returns the index of the first element in an array that pass a test
forEach(function(currentval,[index],[arr]),[thisVal]) // Calls a function for each array element
from(obj,[mapFunc],[thisVal]) // Creates an array from an object
includes(element,[start]) // Check if an array contains the specified element
indexOf(element,[start]) // Search the array for an element and returns its position
isArray(obj) // Checks whether an object is an array
join([seperator]) // Joins all elements of an array into a string
keys() // Returns a Array Iteration Object, containing the keys of the original array
lastIndexOf(element,[start]) // Search the array for an element, starting at the end, and returns its position
map(function(currentval,[index],[arr]),[thisVal]) // Creates a new array with the result of calling a function for each array element
pop() // Removes the last element of an array, and returns that element
push(item1,[...]) // Adds new elements to the end of an array, and returns the new length
reduce(function(total,currentval,[index],[arr]),[initVal]) // Reduce the values of an array to a single value (going left-to-right)
reduceRight(function(total,currentval,[index],[arr]),[initVal]) // Reduce the values of an array to a single value (going right-to-left)
reverse() // Reverses the order of the elements in an array
shift() // Removes the first element of an array, and returns that element
slice([start],[end]) // Selects a part of an array, and returns the new array
some(function(currentval,[index],[arr]),[thisVal]) // Checks if any of the elements in an array pass a test
sort([compareFunc]) // Sorts the elements of an array
splice(index,[quantity],[item1,...]) // Adds/Removes elements from an array
toString() // Converts an array to a string, and returns the result
unshift(item1,...) // Adds new elements to the beginning of an array, and returns the new length
valueOf() // Returns the primitive value of an array
Comment

array method

function square(arr) {
       const newArr = arr.map(x => x * x );
    return newArr ;
  
  //if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
Comment

JavaScript Array Methods

let dailyActivities = ['sleep', 'work', 'exercise']
let newRoutine = ['eat'];

// sorting elements in the alphabetical order
dailyActivities.sort();
console.log(dailyActivities); // ['exercise', 'sleep', 'work']

//finding the index position of string
const position = dailyActivities.indexOf('work');
console.log(position); // 2

// slicing the array elements
const newDailyActivities = dailyActivities.slice(1);
console.log(newDailyActivities); // [ 'sleep', 'work']

// concatenating two arrays
const routine = dailyActivities.concat(newRoutine);
console.log(routine); // ["exercise", "sleep", "work", "eat"]
Comment

array methods in javascript

[1, 2, 3].push(4); // [1, 2, 3, 4]
[1, 2, 3].pop(); // [1, 2]
[1, 2, 3].shift(); // [2, 3]
[1, 2, 3].unshift(0); // [0, 1, 2, 3]
['a', 'b'].concat('c'); // ['a', 'b', 'c']
['a', 'b', 'c'].join('-'); // a-b-c
['a', 'b', 'c'].slice(1); // ['b', 'c']
['a', 'b', 'c'].indexOf('b'); // 1
['a', 'b', 'c'].includes('c'); // true
[3, 5, 6, 8].find((n) => n % 2 === 0); // 6
[2, 4, 3, 5].findIndex((n) => n % 2 !== 0); // 2
//Answer by Anshul soni
//Visit my website- https://anshulsoni.netlify.app/
Comment

Array Methods

//ADD ELEMENTS
//PUSH - adds elements to the END OF ARRAY
const friends = ["Zizi", "Ioseb", "Maiko"];
const newLength = friends.push("Karina");
console.log(friends); //["Zizi", "Ioseb", "Maiko"]
console.log(newLenght); //["Zizi", "Ioseb", "Maiko", "Karina"]

//UNSHIFT - add elements to BEGINNING OF THE ARRAY
friends.unshift("Baqso");
console.log(friends); //["Baqso", "Zizi", "Ioseb", "Maiko", "Karina"]

//REMOVE ELEMENTS
//POP - removes LAST ELEMENT FROM ARRAY
friends.pop(); //["Karina"]
//returns removed element
const popped = friends.pop();
console.log(popped); //["Karina"]
console.log(friends); //["Baqso", "Zizi", "Ioseb", "Maiko"]

//shift - Removes FIRST ELEMENT FROM THE ARRAY
friends.shift(); //["Baqso"]
console.log(friends); //["Zizi", "Ioseb", "Maiko"]

//indexOf - Tells the position of the element in the ARRAY
console.log(friends.indexOf("Zizi")); //0
//if we write the element which is not in the ARRAY we get : -1
console.log(friends.indexOf("zizi")); //-1, we need to write in CamelCase

//includes - modern method of indexOf
//includes - testing with STRICT EQUALITY
console.log(friends.includes("Zizi")); //true


Comment

array methods in javascript

ARRAY INSERTION
Comment

what are array methods in javascript

[1, 2, 3].push(4); // [1, 2, 3, 4]
[1, 2, 3].pop(); // [1, 2]
[1, 2, 3].shift(); // [2, 3]
[1, 2, 3].unshift(0); // [0, 1, 2, 3]
['a', 'b'].concat('c'); // ['a', 'b', 'c']
['a', 'b', 'c'].join('-'); // a-b-c
['a', 'b', 'c'].slice(1); // ['b', 'c']
['a', 'b', 'c'].indexOf('b'); // 1
['a', 'b', 'c'].includes('c'); // true
[3, 5, 6, 8].find((n) => n % 2 === 0); // 6
[2, 4, 3, 5].findIndex((n) => n % 2 !== 0); // 2
[3, 4, 8, 6].map((n) => n * 2); // [6, 8, 16, 12]
[1, 4, 7, 8].filter((n) => n % 2 === 0); // [4, 8]
[2, 4, 3, 7].reduce((acc, cur) => acc + cur); // 16
[2, 3, 4, 5].every((x) => x < 6); // true
[3, 5, 6, 8].some((n) => n > 6); // true
[1, 2, 3, 4].reverse(); // [4, 3, 2, 1]
[3, 5, 7, 8].at(-2); // 7
27
javascript array functions
Comment

Array methods javascript

[1, 2, 3].push(4); // [1, 2, 3, 4]
[1, 2, 3].pop(); // [1, 2]
[1, 2, 3].shift(); // [2, 3]
[1, 2, 3].unshift(0); // [0, 1, 2, 3]
['a', 'b'].concat('c'); // ['a', 'b', 'c']
['a', 'b', 'c'].join('-'); // a-b-c
['a', 'b', 'c'].slice(1); // ['b', 'c']
['a', 'b', 'c'].indexOf('b'); // 1
['a', 'b', 'c'].includes('c'); // true
[3, 5, 6, 8].find((n) => n % 2 === 0); // 6
[2, 4, 3, 5].findIndex((n) => n % 2 !== 0); // 2
[3, 4, 8, 6].map((n) => n * 2); // [6, 8, 16, 12]
[1, 4, 7, 8].filter((n) => n % 2 === 0); // [4, 8]
[2, 4, 3, 7].reduce((acc, cur) => acc + cur); // 16
[2, 3, 4, 5].every((x) => x < 6); // true
[3, 5, 6, 8].some((n) => n > 6); // true
[1, 2, 3, 4].reverse(); // [4, 3, 2, 1]
[3, 5, 7, 8].at(-2); // 7
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery get text 
Javascript :: javascript transpose rows to columns 
Javascript :: form data append jquery 
Javascript :: mongoose generate objectid 
Javascript :: click outside element jquery 
Javascript :: jquery switch class 
Javascript :: javascript radio button onchange 
Javascript :: get buffer from file javascript 
Javascript :: $(getJson) returning error 
Javascript :: makes number negative javascript 
Javascript :: javascript remove all event listeners 
Javascript :: get express variable 
Javascript :: html javascript find data attribute 
Javascript :: install plotly with react 
Javascript :: check if a checkbox is checked jquery 
Javascript :: react native flatlist 
Javascript :: how to master javascript 
Javascript :: httpclientmodule is not an angular module 
Javascript :: find in array react 
Javascript :: keypress javascript 
Javascript :: convertir seconde 
Javascript :: js get current function name 
Javascript :: hide and show modal in jquery 
Javascript :: lodash filter object keys 
Javascript :: base64 to png nodejs 
Javascript :: free robux javascript 
Javascript :: javascript redirect to route laravel 
Javascript :: react update component after api call 
Javascript :: how you can use javascript to play the sound for the button color selected 
Javascript :: jquery change text 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =