Skip to content

Commit 55fea44

Browse files
authored
Updated More Array Methods
1 parent e85a761 commit 55fea44

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

javascript_array_methods.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,155 @@ if (!percentage.includes(15)) {
217217
}
218218
//includes end
219219
/*###########################################################################*/
220+
221+
/*###########################################################################*/
222+
//foreach
223+
const monthlySalary = [16632, 16032, 15723, 16032, 14800, 16900];
224+
const epfDeductionOnSalary = 0.12;
225+
const monthlyEpf = [];
226+
227+
//benefit of foreach loop is we can use index but in forof loop can't
228+
monthlySalary.forEach((salary, idx, monSal) => {
229+
const obj = {
230+
index: idx,
231+
sal: salary,
232+
epfDeduction: salary * epfDeductionOnSalary,
233+
};
234+
monthlyEpf.push(obj);
235+
});
236+
237+
console.log(monthlyEpf);
238+
//foreach end
239+
/*###########################################################################*/
240+
241+
/*###########################################################################*/
242+
//map
243+
const mobliePrices = [20000, 12999.99, 34000, 15999.99, 12999.99, 10500.5];
244+
const gst = 0.18;
245+
246+
//change elements on array and return brand new array or address
247+
const _finalPrice = mobliePrices.map((price, idx, mobPri) => {
248+
const finalMobilePrice = {
249+
index: idx,
250+
finalPrice: price * gst + mobliePrices[idx],
251+
mobPr: mobPri,
252+
};
253+
254+
return finalMobilePrice;
255+
});
256+
257+
console.log(mobliePrices, _finalPrice);
258+
//map end
259+
/*###########################################################################*/
260+
261+
/*###########################################################################*/
262+
//sort and reversing
263+
const names = ["meet", "sumeet", "hareet", "reet", "bhanu", "kavita"];
264+
const numbers = [10, 20, 44, 55, 66, 3.22, 7, -9];
265+
266+
const sortedNames = names.sort();
267+
console.log(sortedNames);
268+
269+
//sort() by default converts everything to a string and then sorts
270+
let sortedNumbers = numbers.sort((a, b) => {
271+
return a - b; //ascending order [1 2 3 4 5]
272+
});
273+
console.log(sortedNumbers);
274+
275+
sortedNumbers = numbers.sort((a, b) => {
276+
return b - a; //descending order [5 4 3 2 1]
277+
});
278+
console.log(sortedNumbers);
279+
280+
//reverse()
281+
const reverseSortedNumbers = sortedNumbers.reverse();
282+
console.log(reverseSortedNumbers);
283+
284+
const reverseNames = names.reverse();
285+
console.log(reverseNames);
286+
//sort and reversing end
287+
/*###########################################################################*/
288+
289+
/*###########################################################################*/
290+
//filter
291+
const percentages = [
292+
70, 80, 75, 45, 65, 73, 90, 95, 55, 54, 36, 62, 61, 59, 87,
293+
];
294+
295+
//filter return condition based array
296+
// const filteredPercentages = percentages.filter((percent) => percent > 60); //arrow fun advantage
297+
const filteredPercentages = percentages.filter((percent, idx, arr) => {
298+
return percent > 60;
299+
});
300+
301+
console.log(filteredPercentages);
302+
//filter end
303+
/*###########################################################################*/
304+
305+
/*###########################################################################*/
306+
//reduce
307+
const __monthlySalary = [
308+
80000, 70000, 65235, 74521, 63254, 98666, 74235, 86666, 63214, 99412, 74215,
309+
105249,
310+
];
311+
312+
//reduce() can reduce and array of values to a simpler value
313+
const sum = __monthlySalary.reduce((preVal, curVal, idx, arr) => {
314+
return preVal + curVal;
315+
}, 0);
316+
317+
console.log(sum);
318+
//reduce end
319+
/*###########################################################################*/
320+
321+
//split and join for strings
322+
/*###########################################################################*/
323+
const data = "Meet;8770851929;Bhopal";
324+
325+
//split() check the delimeter and return the array
326+
const transformedData = data.split(";");
327+
console.log(transformedData);
328+
329+
//join works opposite of split, with the help of separator and join the array into one single string
330+
//it will always generates a string
331+
const fullName = ["Meet", "Kumar", "Vishwakarma"];
332+
const joinName = fullName.join("-");
333+
console.log(joinName);
334+
//split and join for strings end
335+
/*###########################################################################*/
336+
337+
/*###########################################################################*/
338+
//speard operator ...
339+
const computerComponents = ["RAM", "Processor", "Hard Drive", "CPU"];
340+
341+
//speard operator ... pull out elements in an array and
342+
//copied all elements and gives brand new address or reference
343+
const copiedComputerComponents = [...computerComponents];
344+
computerComponents.push("Graphic Card");
345+
console.log(computerComponents, copiedComputerComponents);
346+
347+
console.log(Math.min(...__monthlySalary)); //find min pass array as an arg using speard operator
348+
349+
//when copying the array of objects then copied reference which is hold the objects in an array
350+
const booksNames = [
351+
{ name: "Maths" },
352+
{ name: "Science" },
353+
{ name: "Physics" },
354+
];
355+
356+
//copying the elements but object refereces are still same in the memory
357+
const copiedBookNames = [...booksNames];
358+
console.log(booksNames, copiedBookNames);
359+
360+
//as we know references are still same in an array then changes one object it reflects same to other array
361+
booksNames[0].name = "Mathametics"; //same reference for both arrays
362+
console.log(booksNames, copiedBookNames);
363+
364+
//if want to make a new copy of object
365+
const newCopiedBookNames = [
366+
...booksNames.map((value) => ({ name: value.name })),
367+
];
368+
booksNames[0].name = "Hindi";
369+
console.log(booksNames, newCopiedBookNames); //now newCopiedBookNames is not affected
370+
//speard operator ... end
371+
/*###########################################################################*/

0 commit comments

Comments
 (0)