-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray-examples.js
More file actions
58 lines (47 loc) · 1.51 KB
/
Array-examples.js
File metadata and controls
58 lines (47 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//push : add object to array
let myArr = [1,2,3,4,5];
myArr.push(6);
console.log("pushed ",myArr);
//pop : remove the object from array
myArr.pop();
console.log("pop ",myArr);
//shift : remove 1st element in array
myArr.shift()
console.log("shift ",myArr);
//unshift : add one or more element to beginning of the array
myArr.unshift(0,1);
console.log("unshift ",myArr);
//forEach
myArr.forEach((value,index)=>{
console.log(value,index);
})
//Array destructure
const array = [1, 2, 3, 4, 5];
//method 1
const [oneElm, twoElm, threeElm, fourElm, FiveElm] = array;
console.log("original array ", array);
console.log("destructure array ");
console.log("", oneElm);
console.log("", twoElm);
console.log("", FiveElm);
//method 2
const { 0: oneObj, 1: twoObj, 2: threeObj } = array;
console.log("destructure array object");
console.log("", oneObj);
console.log("", twoObj);
console.log("", threeObj);
//The flat() method concatenates sub-array elements
const arrayWithArr = [1,2,3,[4,[6,5]]];
console.log(arrayWithArr.flat(2));
//some : return true if an of the element meet the condition, otherwise false
let fruits = ["apple","banana","pear","watermelon"];
let some = fruits.some((fruit)=>{
return fruit == 'apple';
});
console.log("some ",some);
//every : return true if all the elements of array meet the condition, otherwise false
let names = ["anil","anand","apruva","amit"];
let every = names.every((name)=>{
return name.startsWith('b');
})
console.log("every ",every);