Header Ads Widget

Responsive Advertisement

array

What is Array?
What is Object?
What are predifined method in Array?
How to add elements?
How to delete elements?
How to replace elements?
Array destructuring

Array iteration:
For each: it executed a provided function for each element in the array. The code syntax is:

Map function: it created a new array with the results of calling a provided function on every element in the original array. Code syntax:
const numbers = [1,2,3,4,5]
const squares = numbers.map((element)=>{
return element*element
})

Filter function: It creates new array with all elements that pass the test implemented by the provided function. Code syntax:

const numbers = [1,2,3,4,5,6,7,8,9];
const evenNumbers = number.filter((element)=> {
return element%2 === 0
})
console.log(evenNumbers

4) array methods:

// push()
// adds one or more elements to the end of the array.
//returns new length of the array.


let myArray = [1,2,3,4,5];
console.log(myArray);
myArray.push(6,7,8);
myArray.push(9);
myArray.push(10);
myArray.push(11);
myArray.push(12);
console.log(myArray);

// pop()
// removes the last element of an array:

let myArray = [1,2,3,4,5,6];
let lastElement = myArray.pop();
console.log(lastElement);
console.log(myArray);

// unshift()
// adds one or more elements to the begining of an array and returns the
new length of the array.

let myArray = [1,2,3,4,5];
console.log(myArray);
myArray.unshift(9,8,7,6,5,4,3,2,1);
console.log(myArray);

// shift():
// removes the first element of an array and returns it.

let myArray = [1,2,3,4,5];
console.log(myArray);
let firstElement = myArray.shift();
console.log(firstElement);
console.log(myArray);

// slice():
// returns a new array that contain a portion of the original array. The portion is
specified by its start and end indices.

let myArray = [1,2,3,4,5,6,7,8,9];
// 0 1 2 3 4 5 6 7 8
console.log(myArray);
let slicedArray = myArray.slice(1,6);
console.log(slicedArray);
console.log(myArray);

// splice()
// removes one or more elements from an array and adds new element at a specified
index.
splice(start, delete count, element to add).

let myArray = [1,2,3,4,5];
// 0 1 2 3 4
console.log(myArray);
myArray.splice(1,0,'a','b','c','d','e','f','g');
console.log(myArray);

//sort():
the sort function by default sorts the array elements in ascending order.

const fruits = [55,43,22,77.53,77.52];
console.log(fruits);
fruits.sort();
console.log(fruits);

Destructuring:
array destructuring is a way of extracting values from an array and assigning them
to variables in a more concise way. It allows you to extract multiple values from an
array and assign them to the individual variables.

const [first,second,third,a,] = [1,2,3,4,5,6,7,8];
console.log(first);5 hmm
console.log(second);
console.log(third);
console.log(a);
console.log([first,second,third,a,])

Iteration:
array iteration is a way to transverse through an array and perform some action on
each element of the array element.
It takes a callback function as an arguement that is executed for each element.

1) forEach():
function executes a provided function once for each array element.

const numbers = [1,2,3,4,5,6,7,8,9];
console.log(numbers);
numbers.forEach((element) =>{
console.log(element);
});

2) map():
this function creates a new array with the results of calling a provided function on
every element in the original array. It returns a new array with the same length as
the original array.

const numbers = [1,2,3,4,5,6,7,8];
console.log(numbers);
const squares = numbers.map((element)=>{
return element * element
});
console.log(squares);

3) filter:
This function creates new array with all elements that pass the test implemented
by the provided function. It returns a new array with the filtered elements.

const numbers = [1,2,3,4,5,6,7,8];
const evenNumbers = numbers.filter((noName)=>{
return noName %2===0 ;
})
console.log(evenNumbers);
*/





How to remove duplicate value fro hmm Array:

For that use new set function👇

There is a spread operator(...) to convert the set object to array

Post a Comment

0 Comments