JS : Array methods

//* methods of array *//

// 1) push(): adds element at end of array
// 2) unshift(): adds element at start of array
// 3) pop(): removes last element of array
// 4) shift(): removes first element of array
// 5) includes(): checks if an element is present in array and return boolean value
// 6) filter(): filters element of array by given condition 
// 7) map(): we can use this method instead of loop 
// 8) reduce(): we can use this method instead of loop
// 9) forEach(): do modification for each element
//10) find(): finds the element by given condition & returns that element
//11) findIndex(): finds the index of element by given condition & returns index of that element 
//12) some(): find element by given condition & return boolean value
//13) every(): find element by given condition & return boolean value
//14) slice(): returns elements between given indexes
//15) reverse(): reverse the elements
//16) flat(): combines sub-arrays of a array and returns a single combined array
//17) join(): joins elements of array with given character
//18) concat(): joins two different arrays and return a single combined array
//19) at(): returns the element by its index
//20) splice(): deletes the elements by given indexes
//21) fill(): fills or replaces the elements
//22) sort(): sorts the elements alphabetically or numerically


// array methods lecture 1 topics:

// 1) at(): returns the element by its index
// 2) includes(): checks if an element is present in array and return boolean value
// 3) reverse(): reverse the elements
// 4) push(): adds element at end of array
// 5) unshift(): adds element at start of array
// 6) pop(): removes last element of array
// 7) shift(): removes first element of array


//* Array methods: Lecture 1 *// 11-sep-2022

//               0         1        2         3
let fruits = ['apple', 'chikoo', 'mango', 'banana']

//1) at(): returs the element by its index

let a1 = fruits.at(0)
console.log(a1)

let a2 = fruits.at(2)
console.log(a2)

console.log(fruits.at(3))

//2) includes(): checks if an element is present in array and return boolean value

let a3 = fruits.includes('mango')
console.log(a3)

let a4 = fruits.includes('chikoo')
console.log(a4)

let a5 = fruits.includes('pineapple')
console.log(a5)

//3) reverse(): reverse the elements 

let numbers = [1,2,3,4,5]

let a6 = numbers.reverse()
console.log(a6)

//4) push(): adds element at end of array

let a7 = numbers.push('kedar')
console.log(a7)
console.log(numbers)

numbers.push('ashish')
console.log(numbers)

//5) unshift(): adds element at start of array

numbers.unshift('tuljapur')
console.log(numbers)

//6) pop(): removes last element of array

numbers.pop()
console.log(numbers)

//7) shift(): removes first element of array

numbers.shift()
console.log(numbers)

numbers.shift()
console.log(numbers)


// array methods lecture 2 topics:

// 8)   forEach(): do modification for each element
// 9)   find(): finds the asked element & returns that element
// 10) findIndex(): finds the index of asked element & returns index of that element 
// 11) some(): finds the asked element & return boolean value
// 12) every(): finds the asked element & return boolean value
// 13) filter(): filters elements of array by given condition

//* Array methods: Lecture 2 *// 12-sep-2022

//                          0             1             2          3
let batsmen = ['sachin', 'sehwag', 'dravid', 'kohli']

// 8)  forEach(): do modification for each element & returns modified elements

batsmen.forEach(function(el,index,arr){
    console.log('hi '+ el)
})

// 9)  find(): finds & returns the first element matching given condition

//               0  1  2  3  4  5
let numbers2 = [10,20,30,40,50,60]

let z1 = numbers2.find(function(el,index,arr){
    return el > 40
})
console.log(z1)

let z2 = numbers2.find(function(el,index,arr){
    return el < 30
})
console.log(z2)

// 10) findIndex(): finds & returns index of first element matching given condition 

let z3 = numbers2.findIndex(function(el){
    return el > 10
})
console.log(z3)

let z4 = numbers2.findIndex(function(el){
    return el > 40
})
console.log(z4)

// 11) some(): finds the asked element & return boolean value

 let numbers3 = [2,4,6,8,10,12]

let z5 = numbers3.some(function(el){
    return el > 4
})
console.log(z5)

let z6 = numbers3.some(function(el){
    return el < 2
})
console.log(z6)

// 12) every(): finds the asked element & return boolean value

let numbers4 = [4,8,12,16,20,24]

let z7 = numbers4.every(function(el){
    return el > 2
})
console.log(z7)

let z8 = numbers4.every(function(el){
    return el > 10
})
console.log(z8)

// 13) filter(): filters elements of array by given condition

let numbers5 = [100,110,120,130,140,150,160]

let z9 = numbers5.filter(function(el){
    return el > 120
})
console.log(z9)

let z10 = numbers5.filter(function(el){
    return el < 130
})
console.log(z10)


// array methods lecture 3 topics:

// 14) flat(): combines sub-arrays of a array and returns a single combined array
// 15) join(): joins elements of array with given character & returns array
// 16) concat(): joins two different arrays and return a single combined array
// 17) fill(): fills or replaces the elements & returns array
// 18) sort(): sorts the elements alphabetically or numerically & returns array

//* Array methods: Lecture 3 *// 13-sep-2022

// 14) flat(): combines sub-arrays of a array and returns a single combined array

// let birds = [['sparrow','hen'],['peacock','eagle'],['kite','pigeon']]

// let v1 = birds.flat()
// console.log(v1)


// 15) join(): joins elements of array with given character

// let info = ['Akshay', 'Kumar', '9900120012']

// let v2 = info.join('-')
// console.log(v2)

// let v3 = info.join('@')
// console.log(v3)


// 16) concat(): joins two different arrays and return a single combined array
//                0        1        2
// let movies1 = ['sholey','deewar', 'kgf']
// let movies2 = ['gadar','dabang']

// let v4 = movies1.concat(movies2)
// console.log(v4)


// 17) fill(): fills or replaces the elements

// let v5 = movies1.fill('#',0,2)

// 18) sort(): sorts the elements alphabetically or numerically

// let names10 = ['ashish', 'shravan', 'vaibhav', 'sham', 'balu']
// let v6 = names10.sort()
// console.log(v6)

// let numbers10 = [4,6,8,7,9,5,3,2,1]
// let v7 = numbers10.sort()
// console.log(v7)


// array methods lecture 4 topics:

// 19) map(): we can use this method instead of loop 
// 20) reduce(): we can use this method instead of loop
// 21) slice(): returns elements between given indexes
// 22) splice(): deletes the elements by given indexes

//* Array methods: Lecture 4 *// 14-sep-2022

// 19) map(): perform function with each element and return elements in array

// let numbers11 = [11,22,33,44]

// let s1 = numbers11.map(function(el){
//     return 100 - el
// })
// console.log(s1)

// let s2 = numbers11.map(function(el){
//     return 2022 - el
// })
// console.log(s2)

// 20) reduce(): can be used to make sum of elements

// let numbers12 = [12,25,36,47]

// let s3 = numbers12.reduce(function(acc,el){
//     return acc + el
// },0)
// console.log(s3)

// 21) slice(): returns elements between given indexes in an array

//                0            1          2        3        4
// let cities = ['tuljapur', 'dharashiv','solapur', 'pune', 'mumbai']

// let s4 = cities.slice(0,1)
// console.log(s4)

// let s5 = cities.slice(0,2)
// console.log(s5)

// let s6 = cities.slice(1,4)
// console.log(s6)

// 22) splice(): deletes the elements by given indexes & returns deleted element in array

//                0           1          2        3
// let cities2 = ['nagpur', 'bangalore', 'delhi', 'bhopal']

// let s7 = cities2.splice(0,1)
// console.log(s7)

//     0        1      2
// [banglore, delhi, bhopal]  <--- updated array

// let s8 = cities2.splice(0,2)
// console.log(s8)

Comments

Popular posts from this blog

What is IP and TCP - Marathi

JS - Day 1

JS - basic types and ways of writing function