Skip to content

Commit a35d9c2

Browse files
committed
array methods
1 parent 97d45ba commit a35d9c2

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

server/arrayMethodsES6Mod.mjs

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
const log = console.log;
2+
3+
export function concat() {
4+
// Create arrays of monovalves and bivalves
5+
let monovalves = ['abalone', 'conch'];
6+
let bivalves = ['oyster', 'mussel', 'clam'];
7+
8+
// Concatenate them together into shellfish variable
9+
let shellfish = monovalves.concat(bivalves);
10+
log(shellfish);
11+
}
12+
13+
export function join() {
14+
let fish = ['piranha', 'barracuda', 'koi', 'eel'];
15+
// Join the elements of an array into a string
16+
17+
// If separator omitted, the array elements are separated with a comma.
18+
let fishString = fish.join();
19+
log(fishString);
20+
21+
let fishStringWithHyphen = fish.join('-');
22+
log(fishStringWithHyphen);
23+
}
24+
25+
export function slice() {
26+
let fish = ['piranha', 'barracuda', 'koi', 'eel'];
27+
// Slice a new array from 2 to 4
28+
let fishWithShortNames = fish.slice(2, 4);
29+
log(fishWithShortNames);
30+
31+
// Slice a new array from 2 to the end of the array
32+
fishWithShortNames = fish.slice(2);
33+
34+
log(fishWithShortNames);
35+
}
36+
37+
export function indexOf() {
38+
let fish = ['piranha', 'barracuda', 'koi', 'barracuda'];
39+
// Find the first instance of an element
40+
let index = fish.indexOf('barracuda');
41+
log(index);
42+
43+
//If the given argument is a value that does not exist in the array, it will return -1.
44+
index = fish.indexOf('akash');
45+
log(index);
46+
}
47+
48+
export function lastIndexOf() {
49+
let fish = ['piranha', 'barracuda', 'koi', 'barracuda'];
50+
// Find the first instance of an element
51+
let index = fish.lastIndexOf('barracuda');
52+
log(index);
53+
54+
//If the given argument is a value that does not exist in the array, it will return -1.
55+
index = fish.lastIndexOf('akash');
56+
log(index);
57+
}
58+
59+
export function pop() {
60+
const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
61+
log('fruits before pop => ', fruits);
62+
let poppedFruit = fruits.pop();
63+
log('fruits after pop', fruits);
64+
65+
// The pop() method returns the value that was "popped out":
66+
log('popped Fruit => ', poppedFruit);
67+
}
68+
69+
export function push() {
70+
const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
71+
log('fruits before PUSH => ', fruits);
72+
let newLength = fruits.push('KIWI');
73+
log('fruits after PUSH => ', fruits);
74+
75+
// The push() method returns the new array length:
76+
log('popped Fruit => ', newLength);
77+
}
78+
79+
export function shift() {
80+
// The shift() method removes the first array element and "shifts" all other elements to a lower index.
81+
82+
const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
83+
log('fruits before shift => ', fruits);
84+
fruits.shift();
85+
log('fruits after shift => ', fruits);
86+
}
87+
88+
export function unshift() {
89+
// The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:
90+
91+
const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
92+
log('fruits before unshift => ', fruits);
93+
let newLength = fruits.unshift('KIWI');
94+
log('fruits after unshift => ', fruits);
95+
96+
// The unshift() method returns the new array length.
97+
log('NEW LENGTH AFTET UNSHIFT => ', newLength);
98+
}
99+
100+
export function deleteElement() {
101+
// The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:
102+
103+
const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
104+
log('fruits before delete => ', fruits);
105+
delete fruits[0];
106+
log('fruits after delete => ', fruits);
107+
}
108+
109+
export function addWithSplice() {
110+
// splice(index number, number of items to remove, items to add)
111+
112+
let fish = ['piranha', 'barracuda', 'koi', 'eel'];
113+
114+
// Splice a new item number into index position 1
115+
fish.splice(1, 0, 'manta ray');
116+
117+
log(fish);
118+
}
119+
120+
export function removeWithSplice() {
121+
// splice(index number, number of items to remove, items to add)
122+
let fish = ['piranha', 'barracuda', 'koi', 'eel'];
123+
124+
// Remove two items, starting at index position 1
125+
fish.splice(1, 2);
126+
127+
log(fish);
128+
}
129+
130+
export function addremoveWithSplice() {
131+
// splice(index number, number of items to remove, items to add)
132+
let fish = ['piranha', 'barracuda', 'koi', 'eel'];
133+
134+
// Remove two items and add one
135+
fish.splice(1, 2, 'manta ray');
136+
137+
log(fish);
138+
139+
fish.splice(1, 0, 'first-fish', 'second-fish');
140+
log(fish);
141+
}
142+
143+
export function fill() {
144+
let fish = ['piranha', 'barracuda', 'koi', 'eel'];
145+
146+
// Replace all values in the array with "shark"
147+
fish.fill('shark');
148+
149+
log(fish);
150+
}
151+
152+
export function sort() {
153+
let fish = ['piranha', 'barracuda', 'koi', 'eel'];
154+
// Sort items in array
155+
fish.sort();
156+
log(fish);
157+
158+
//Since sort() is based on the first unicode character, it will sort uppercase items before lowercase.
159+
fish = ['piranha', 'barracuda', 'Koi', 'eel'];
160+
fish.sort();
161+
log(fish);
162+
163+
//Numbers come before both uppercase and lowercase characters.
164+
fish = ['piranha', 'barracuda', 'Koi', '1 eel'];
165+
fish.sort();
166+
log(fish);
167+
168+
// sort() will not sort an array of numbers by size by default. Instead, it will only check the first character in the number.
169+
let numbers = [42, 23, 16, 15, 4, 8];
170+
numbers.sort();
171+
log(numbers);
172+
173+
// In order to sort numbers properly, you could create a comparison function as an argument.
174+
// Function to sort numbers by size
175+
const sortNumerically = (a, b) => {
176+
return a - b;
177+
};
178+
numbers.sort(sortNumerically);
179+
log(numbers);
180+
log(numbers.sort((a, b) => b - a));
181+
182+
//object array sort
183+
let employees = [
184+
{ name: 'John', salary: 90000, hireDate: new Date(2011, 10, 30) },
185+
{ name: 'David', salary: 75000, hireDate: new Date(2011, 11, 30) },
186+
{ name: 'Ana', salary: 80000, hireDate: new Date(2022, 10, 30) },
187+
];
188+
employees.sort(function (x, y) {
189+
return x.salary - y.salary;
190+
});
191+
192+
console.table(employees);
193+
194+
// by name
195+
employees.sort(function (x, y) {
196+
let a = x.name.toUpperCase(),
197+
b = y.name.toUpperCase();
198+
return a == b ? 0 : a > b ? 1 : -1;
199+
});
200+
201+
console.table(employees);
202+
203+
// by date
204+
employees.sort(function (x, y) {
205+
let a = new Date(x.hireDate),
206+
b = new Date(y.hireDate);
207+
return a - b;
208+
});
209+
210+
console.table(employees);
211+
}

server/indexEs6.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,26 @@ console.log(add(12, 3));
1111
console.log(defaultExportedFunction(12, 3));
1212

1313
console.log(calculator.division(12, 3));
14+
15+
//////////////////////////====ARRAY METHODS=====//////////////////////////////////////////
16+
17+
import * as arrayMethods from './arrayMethodsES6Mod.mjs';
18+
19+
// https://www.digitalocean.com/community/tutorial_series/working-with-arrays-in-javascript
20+
// https://www.youtube.com/watch?v=80KX6aD9R7M&list=PLnHJACx3NwAfRUcuKaYhZ6T5NRIpzgNGJ
21+
22+
arrayMethods.concat();
23+
arrayMethods.join();
24+
arrayMethods.slice();
25+
arrayMethods.indexOf();
26+
arrayMethods.lastIndexOf();
27+
arrayMethods.pop();
28+
arrayMethods.push();
29+
arrayMethods.shift();
30+
arrayMethods.unshift();
31+
arrayMethods.deleteElement();
32+
arrayMethods.addWithSplice();
33+
arrayMethods.removeWithSplice();
34+
arrayMethods.addremoveWithSplice();
35+
arrayMethods.fill();
36+
arrayMethods.sort();

0 commit comments

Comments
 (0)