Topic 4: JS array & methods of array - 14 to 22

//Revision Lecture 6: 30 Sep 2022

//Topic 4: //* JS array & methods of array *//


//methods of array 14 to 22:


// 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)

let j2 = movies2.concat(movies1)
console.log(j2)


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

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


// 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)


// 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)
console.log(cities2)

//---------------------

//              0       1        2        3         4
let colors = ['red', 'black', 'white', 'yellow', 'purple']

let k1 = colors.slice(0,2)
console.log(k1)

let k2 = colors.slice(1,4)
console.log(k2)

let k3 = colors.slice(2,6)
console.log(k3)

let k4 = colors.slice(1)
console.log(k4)

टिप्पण्या