Return to site

javascript array api

👉push, pop, shift, unshift, concat, join, slice, indexOf, includes, find, findIndex, map, filter, reduce, every, some, reverse, at

· fullstack
broken image

// PUSH

let array =[1, 2, 3]

array.push(4)

console.log(array) // [1,2,3,4]

 

// POP

array=[1, 2, 3]

array.pop()

console.log(array)// [1,2]

 

// SHIFT

array=[1, 2, 3]

array.shift()

console.log(array)// [2,3]

 

// UNSHIFT

array=[1, 2, 3]

array.unshift(0)

console.log(array)// [0,1,2,3]

 

// CONCAT

array=['a', 'b']

array=array.concat('c')

console.log(array)// ['a','b','c']

 

// JOIN

array = ['a', 'b', 'c']

console.log(array.join('-') )// a-b-c

 

// SLICE

array = ['a', 'b', 'c']

array=array.slice(1)

console.log(array)// ['b','c']

 

// INDEXOF

array = ['a', 'b', 'c']

console.log(array.indexOf('b')) // 1

 

// INCLUDES

array = ['a', 'b', 'c']

console.log(array.includes('c')) // true

 

// FIND

array=[3, 5, 6, 8]

console.log(array.find((n) => n % 2 === 0)) // 6

 

// FINDINDEX

array=[6, 4, 3, 5]

console.log(array.findIndex((n) => n % 2 !== 0)) // 2

 

//MAP

array=[3, 4, 8, 6]

array=array.map((n) => n * 2)

console.log(array) // [6,8,16,12]

 

// FILTER

array=[1, 4, 7, 8]

array=array.filter((n)=> n % 2 === 0)

console.log(array)// [4,8]

 

// REDUCE

array=[2, 4, 3, 7]

console.log(array.reduce((acc, cur) => acc + cur)) // 16

 

// EVERY

array=[2, 3, 4, 5]

console.log(array.every((x) => x < 6)) // true

 

// SOME

array=[3, 5, 6, 8]

console.log(array.some((n) => n > 6)) // true

 

// REVERSE

array=[1, 2, 3, 4]

console.log(array.reverse())// [4,3,2,1]

 

// AT

array=[3, 5, 7, 8]

console.log(array.at(-2 )) // 7