-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayMethod.js
181 lines (142 loc) · 4.51 KB
/
arrayMethod.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Array
let arr = [34,45,67,89,90,56,78,90,23,45] // array which group of the variable
console.log(arr,typeof(arr)) //which is object
//Array constructor
//main point about the array that sort m
let arr1 = new Array(7) //parameter which is length
// here intialize to array size which creating 7 length but
// main things that here empty variables add
console.log(arr1)
// Array(7) [ <7 empty slots> ]
// array method
// concat method
// const array1 = ['a', 'b', 'c'];
// const array2 = ['d', 'e', 'f'];
// const array3 = array1.concat(array2);
// console.log(array3);
// entries
// const iterator1 = arr.entries();
// // return the iterator
// console.log(iterator1.next().value) //[ 0, 34 ]
// console.log(iterator1.next().value)
// filter
// const eventArr = arr.filter(e => e % 2 == 0);
// const oddArr = arr.filter(e => e % 2 !== 0);
// console.log(eventArr);
// console.log(oddArr);
// find()
const finde = arr.find(e=>e>50)
console.log(finde) //67
// flatMap()
// return array which is depth one to convert the all subarray to element
// const arr2 = [1, 2, [3], [4, 5], 6, [[[5,6,[90,[5,7,9]]]]]];
// const flatmap = arr2.flatMap(num => num);
// console.log(flatmap);
// Array.from()
// create new array shallow-copied array instance from given existence array
const charArr = Array.from("jayendra");
console.log(charArr)
// includes()
console.log(arr.includes(45));
// join()
const joinStr = arr.join('-');
console.log(joinStr); //34-45-67-89-90-56-78-90-23-45 return as string
// map
const nums = [22,78,11,90,3,23,909,233,898]
const numsmap = nums.map((e)=> e * 2);
console.log(numsmap);
//reduce(callback(accumulator,currentvalue,currentindex)) method
const nums2 = [0,1,2,3,4]
let intialvalue = 1;
const sumofelement = nums2.reduce((acc,e,i)=> acc + e,intialvalue);
console.log(sumofelement);
// find the occurance of given element
const fruits = [ 'Banana', 'Orange', 'Apple', 'Orange', 'Pear', 'Banana']
const occurrences = fruits.reduce((acc, currFruit) => {
//spreadopertor
return {...acc, [currFruit]: (acc[currFruit] || 0) + 1 }
}, {})
console.log(occurrences)
// second example
//indivdiual element that to sort by highest frequency to lower
// const nums3 = ['abbced','jayedra','locked','bccedprt']
// const nums4 = nums3.reduce((acc,element)=>{
// const frArr = element.reduce((acc,currchar)=>{
// return {...acc,[currchar]:(acc[currchar] || 0) + 1}
// },{})
// console.log(frArr);
// })
const students = [
[ "Kingsley", 70 ],
[ "Jack", 80 ],
[ "Joe", 63 ],
[ "Beth", 75 ],
[ "Kareem", 59 ],
[ "Sarah", 93]
]
// to divided the name and score in seperate array using reduce property
// const names = students.reduce((acc,student)=> [...acc, { student[0] : student[1]} ],{})
// console.log(names);
// slice(start,end) return array of the shallow copy given element
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
// // console.log(anarr);
// var anarr = animals.slice(2,4);
// anarr[0] = 'lion'
// console.log(animals,anarr);
// splice
// splice(start)
// splice(start, deleteCount)
// splice(start, deleteCount, item1)
// splice(start, deleteCount, item1, item2, itemN)
// inserting element
let firstan = animals.splice(0,0,'lion')
console.log(animals)
// delete element
animals.splice(2,1);
console.log(animals)
//delete two element and add in index 1 to three element
animals.splice(1,2,'tiger','fox','wolvern')
console.log(animals)
//main point about the array that sort method sort the string type which lexicographic order
// ex :
let array = [567,23,12,100,34,20000,3000];
array.sort();
// console.log(array);
// Array(7) [ 100, 12, 20000, 23, 3000, 34, 567 ]
// therefore use to callback in sort method
array.sort((a,b)=> a-b);
console.log(array);
// Array(7) [ 12, 23, 34, 100, 567, 3000, 20000 ]
//array destructing
let array4 = ['val1','val2','val3','val4','val5'];
let [val1,val2,...array5] = array4;
console.log(val1,val2,array5)
// temporal dead zone
// console.log(a)
// let a
// set
const nums5 = [45,34,2,1,2,7,8,45,78,20,10]
// const set = new Set(nums5);
const set = new Set();
set.add(2)
set.add(2)
set.add(2)
set.add(2)
set.add(2)
set.add(5)
set.add(8)
console.log(set);
// check whether element present or not
console.log(set.has(5));
// Map()
// key value
const map = new Map();
map.set("firstName","jayendra");
map.set("age",21);
map.set("lastName","parmar");
for(const key of map){
// console.log(key,value)
console.log(key,Array.isArray(key))
}
// get the value
console.log(map.get("age"));