"Continuing the MERN Journey: Mastering JavaScript Arrays for Efficient Data Handling!"

"Continuing the MERN Journey: Mastering JavaScript Arrays for Efficient Data Handling!"

Arrays are data structures used to store multiple pieces of similar or diverse data under a single variable name in programming. They provide a convenient way to organize and manage data, whether it's a list of numbers, strings, or a mix of different types. Arrays can hold homogeneous data types (e.g., all numbers) or heterogeneous types (e.g., numbers and strings), making them versatile tools for developers to work with data efficiently.

Rest Operator-> The rest parameter accepts or allows a function to take an infinite numbers of arguments as an array

Or it accepts multiple arguments at a time

Types of Function used in Arrays

1.Array toString()

2.Array pop()

3.Array delete()

4.Array splice()

5.Array slice()

6.Array filter()


//Array Filter()
let number = [10,12,30,-1]
const postiveNumber = number.filter(
    (value) => {return (value != 30)
}
)  
console.log(postiveNumber) //[ 10, 12, -1 ]

//Array push
const fruit = ["apple","banna","orange"]
fruit.push("lichi","mango")
console.log(fruit) //[ 'apple', 'banna', 'orange', 'lichi', 'mango' ]


//Merging of two arrays     
const fruit = ["apple","banna","orange"]
const place = ["delhi","mumbai","gurugram"]
const person =  fruit.concat(place)
console.log(person)
// output[ 'apple', 'banna', 'orange', 'delhi', 'mumbai', 'gurugram' ]

//Merge 3 or more array
const fruit = ["apple","banna","orange"]
const place = ["delhi","mumbai","gurugram"]
const day = ["monday","tuesday","wednesday"]
const day1 = ["monday1","tuesday2","wednesday3"]
const person =  fruit.concat(place,day,day1) 
//output 
[ 'apple',     'banna',
  'orange',    'delhi',
  'mumbai',    'gurugram',
  'monday',    'tuesday',
  'wednesday', 'monday1',
  'tuesday2',  'wednesday3'
]

//Array Splice()
//To add -> Here 1 is denote as the index number to be added the data 
const fruit = ["apple","banna","orange"]
fruit.splice(1,0,"mango","kiwi")
console.log(fruit)
//output
[ 'apple', 'mango', 'kiwi', 'banna', 'orange' ]

//Delete
// 1(first element) represent  from which index number we have 
//to start deleting the element 
//And second represent to at which we have stop deletion 
let fruit = ["apple","banna","orange","mango","kiwi"]
fruit.splice(1,3)
console.log(fruit) //[ 'apple', 'kiwi' ]

//Array Slice
let fruit = ["apple","banna","orange","mango","kiwi"]
fruit = fruit.slice(1)
console.log(fruit)

//Array filter
const arr = [5,45,2,6]
const result = arr.filter((x) => {return x>2})
console.log(result) //[ 5, 45, 6 ]

//Arrays And Spread Operators
Let array2 = [2,3,4,6]
Let array1 = [4,83,4]
Let arr3 =[...array2 ,...array1 ] 


Console.log (arr3)
<!-- [2,3,4,6] -->

let array2 = [2,3,4,5]
let array1 = [4,83,4]

let arr3 =[...array2 ,...array1] //spread operators

console.log (arr3)


let array2 = [2,3,4,5]
let array1 = [4,83,4]

let arr3 =[...array2 ,...array1] //spread operators

console.log (arr3[0])