Skip to content

Commit d89105d

Browse files
Array Methods
1 parent b3c399b commit d89105d

File tree

12 files changed

+336
-39
lines changed

12 files changed

+336
-39
lines changed

6. Types in JavaScript/13.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ JavaScript Array Methods
1313
8.JavaScript find()
1414
9.JavaScript findIndex()
1515
10.JvaScript flat()
16+
1617
11.JvaScript flatMap()
1718
12.J.vaScript forEach()
1819
13.JavaScript from()

6. Types in JavaScript/18.js

+41-39
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
11
// Array Every()
22

33
const arr = [
4-
{
5-
name: "Luke Skywalker",
6-
height: 172,
7-
mass: 77,
8-
eye_color: "blue",
9-
gender: "male",
10-
},
11-
{
12-
name: "Darth Vader",
13-
height: 202,
14-
mass: 136,
15-
eye_color: "yellow",
16-
gender: "male",
17-
},
18-
{
19-
name: "Leia Organa",
20-
height: 150,
21-
mass: 49,
22-
eye_color: "brown",
23-
gender: "female",
24-
},
25-
{
26-
name: "Anakin Skywalker",
27-
height: 188,
28-
mass: 84,
29-
eye_color: "blue",
30-
gender: "male",
31-
},
4+
{
5+
name: "Luke Skywalker",
6+
height: 172,
7+
mass: 77,
8+
eye_color: "blue",
9+
gender: "male",
10+
},
11+
{
12+
name: "Darth Vader",
13+
height: 202,
14+
mass: 136,
15+
eye_color: "yellow",
16+
gender: "male",
17+
},
18+
{
19+
name: "Leia Organa",
20+
height: 150,
21+
mass: 49,
22+
eye_color: "brown",
23+
gender: "male",
24+
},
25+
{
26+
name: "Anakin Skywalker",
27+
height: 188,
28+
mass: 84,
29+
eye_color: "blue",
30+
gender: "male",
31+
},
3232
];
3333

3434
for (let i of arr) {
35-
if (i.eye_color == "blue") {
36-
console.log(i.eye_color,i.name);
37-
38-
39-
}
40-
41-
35+
if (i.eye_color == "blue") {
36+
console.log(i.eye_color, i.name);
37+
}
4238
}
4339

4440
for (let j = 0; j <= arr.length - 1; j++) {
45-
if (arr[j].eye_color == "blue") {
41+
if (arr[j].eye_color == "blue") {
42+
console.log(arr[j].eye_color, arr[j].name);
43+
}
44+
}
4645

46+
const masss = arr.every((e) => e.mass > 10);
4747

48+
console.log(masss);
4849

49-
console.log(arr[j].eye_color, arr[j].name);
50-
}
50+
const heights = arr.every((e) => e.height <= 202);
51+
console.log(heights);
5152

52-
}
53+
const males = arr.every((e) => e.gender == "male");
54+
console.log(males);

6. Types in JavaScript/19.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Array Fill()
2+
3+
const arr = [1, 2, 3, 4];
4+
console.log(arr);
5+
6+
arr.fill(10);
7+
console.log(arr);
8+
9+
const arr2 = ["Suhail", "Roushan", "Ali"];
10+
console.log(arr2);
11+
12+
arr2.fill("Hello World");
13+
console.log(arr2);
14+
15+
const arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
16+
console.log(arr3);
17+
arr3.fill(1, -2);
18+
console.log(arr3);
19+

6. Types in JavaScript/20.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Array Filter()
2+
3+
const arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10];
4+
// console.log(arr);
5+
// arr.filter((e) => {
6+
// if (e % 2 == 0) {
7+
// console.log(e);
8+
// }
9+
// });
10+
11+
const isEven = (num) => {
12+
if (num % 2 == 0) {
13+
return num;
14+
}
15+
};
16+
17+
let news = arr.filter(isEven);
18+
// console.log(news);
19+
20+
const words = [
21+
"Suhail",
22+
"Roushan",
23+
"Apple",
24+
"Mango",
25+
"Android",
26+
"Samsung",
27+
"Ronak",
28+
"Man",
29+
"Sun",
30+
"Suzuki",
31+
];
32+
function search(num) {
33+
for (let i of words) {
34+
// console.log(i);
35+
let arr = [];
36+
arr.push(i);
37+
if (arr[0][0] == num[0]) {
38+
console.log(arr.join(""));
39+
}
40+
}
41+
}
42+
// search("A");
43+
44+
const prices = [1800, 2000, null, 3000, 5000, "Thousand", 500, 8000];
45+
46+
let ushar = prices.filter((e) => {
47+
if (e > 2000 && e != null) {
48+
console.log(e);
49+
}
50+
});

6. Types in JavaScript/21.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Array Find ()
2+
// It Only Returns the First Values from Array
3+
// const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
4+
5+
// const evenfind = arr.find((num) => num % 2 == 0);
6+
// console.log(evenfind);
7+
8+
// // Array filter filters the whole array
9+
// const even = arr.filter((num) => !!(num & 1));
10+
11+
// console.log(even);
12+
13+
const team = [
14+
{ name: "Bill", age: 10 },
15+
{ name: "Linus", age: 15 },
16+
{ name: "Alan", age: 20 },
17+
{ name: "Steve", age: 34 },
18+
];
19+
20+
let find = team.find((e) => e.age > 10);
21+
console.log(find); // Linus
22+
23+
let find1 = team.filter((e) => e.age > 10);
24+
console.log(find1);
25+
// All Greater than 10
26+
// [
27+
// { name: "Linus", age: 15 },
28+
// { name: "Alan", age: 20 },
29+
// { name: "Steve", age: 34 },
30+
// ];

6. Types in JavaScript/22.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Find Ki Index (Only First Element ka Index)
2+
// The findIndex() method returns the index of the
3+
// first array element that satisfies the provided
4+
// test function or else returns -1.
5+
6+
// If the element does not match it will send -1
7+
const arr = [2, 8, 10, 31, 4];
8+
9+
let ok = arr.findIndex((e) => e % 2 != 0);
10+
console.log(ok);
11+
12+
let days = ["Sunday", "Wednesday", "Tuesday", "Friday"];
13+
14+
let ok2 = days.findIndex((e) => e == "Wed");
15+
console.log(ok2);

6. Types in JavaScript/23.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Array Flat ()
2+
3+
const arr = [1, 2, [3, 4], [5, 6], [7, 8]];
4+
// 1 2 3
5+
let ok = arr.flat(); // Removes all nested arrays
6+
// console.log(ok);
7+
8+
const arr1 = [1, 2, [3, [4, 5]]];
9+
let ok1 = arr1.flat(1); // Removes 1 Array [] Literal
10+
console.log(ok1);
11+
12+
const arr2 = [1, 2, [3, [4, 5, [6, 7]]]];
13+
let ok2 = arr2.flat(2); // Removes 2 Array [] Literal
14+
console.log(ok2);
15+
16+
const arr3 = [1, 2, [3, [4, 5, [6, 7, [8, 9]]]]];
17+
let ok3 = arr3.flat(3); // Removes 3 Array [] Literal
18+
console.log(ok3);
19+
20+
const holes = [1, 2, 3, , 4, , 5, ,];
21+
console.log(holes.flat()); // Removed all empty values
22+
23+
const hey = [1, 2, [3, 4, [5, 6]]];
24+
console.log(hey.flat(2));

6. Types in JavaScript/24.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Array FlatMap ()
2+
3+
// The flatMap() method first maps each element of an
4+
// array using a mapping function,
5+
// then flattens it into a new array.
6+
7+
const arr = [1, 2, 3, 4, 5];
8+
9+
let search = arr.flatMap((e) => e ** 2);
10+
console.log(search);
11+
12+
13+
/*
14+
flatMap() Return Value
15+
Returns a new array after mapping every element using callback
16+
Notes:
17+
18+
The flatMap() method does not change the original array.
19+
The flatMap() method is equivalent to array.map().flat().
20+
21+
22+
23+
*/
24+
25+
// defining an array
26+
let numbers = [1, 2, 3, 4, 5];
27+
28+
// incrementing each element of array using map()
29+
let afterMapping = numbers.map((element) => element + 2);
30+
31+
// flattening the array using flat()
32+
console.log(afterMapping);
33+
34+
let afterFlattening = afterMapping.flat();
35+
36+
console.log(afterFlattening); // [ 3, 4, 5, 6, 7 ]
37+
38+
// using flatMap() instead of flat() and map()
39+
let after_flatMap = numbers.flatMap((element) => element + 2);
40+
41+
console.log(after_flatMap); // [ 3, 4, 5, 6, 7 ]
42+

6. Types in JavaScript/25.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// ForEach ()
2+
3+
const arr = [
4+
"Java",
5+
"JavaScript",
6+
"Python",
7+
"C",
8+
"C++",
9+
"Kotlin",
10+
"Dart",
11+
"Bash",
12+
"C#",
13+
"Swift",
14+
"GO",
15+
"R",
16+
"PHP",
17+
];
18+
19+
console.log(arr);
20+
console.log("Using For Loop");
21+
22+
for (let i = 0; i <= arr.length - 1; i++) {
23+
console.log(arr[i]);
24+
}
25+
console.log("Using For Each");
26+
arr.forEach((i, index) => {
27+
console.log(index + 1, i);
28+
});
29+
30+
const arr1 = [10, 20, 30, 40, 50];
31+
arr1.forEach((i, index, arr) => {
32+
console.log(index + 1, i);
33+
});

6. Types in JavaScript/26.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Array.from()
2+
3+
// The from() method creates a new array from any array-like or iterable object.
4+
5+
// creating a new array from string
6+
7+
// Uses of Array.from ()
8+
/*
9+
1.String to Array "12142" ==> ["1","2","1","4","2"]
10+
11+
The from() method creates a new array from any array-like or iterable object.
12+
Parameters: This method accepts three parameters as mentioned above and described below:
13+
14+
object: This parameter holds that object that will convert into an array
15+
16+
mapFunction: This parameter is optional used to call on each item of the array.
17+
18+
thisValue: This parameter is optional, it holds the context to be passed as this to
19+
be used while executing the mapFunction. If the context is passed, it will be used
20+
like this for each invocation of the callback function, otherwise undefined is used as default.
21+
22+
Return value: It returns a new Array instance whose elements are same as the given array.
23+
In the case of a string, every alphabet of the string is converted to an element of the new array
24+
instance. Below example illustrate the Array from() method in JavaScript:
25+
26+
27+
Example
28+
*/
29+
const arr = "12142";
30+
console.log(Array.from(arr, Number)); // [ '1', '2', '1', '4', '2' ]
31+
32+
// 2nd Parameter takes map
33+
// console.log(Array.from(arr, (x) => Number(x)));

6. Types in JavaScript/27.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Array Includes()
2+
3+
// Array Includes value in true or false
4+
// The includes() method returns:
5+
6+
// true if searchValue is found anywhere within the array
7+
// false if searchValue is not found anywhere within the array
8+
9+
// Array Includes check the given element is
10+
// present in the array or not
11+
//
12+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
13+
14+
console.log(arr.includes(11)); // false
15+
console.log(arr.includes(4)); // true
16+
// 11 is not in the array so it gave false
17+
// 4 is in the array so it gave true
18+
19+
// The includes() method is case sensitive. For example:
20+
21+
const words = ["Java", "Python", "C"];
22+
console.log(words.includes("c")); // false
23+
// There is no lowerCase c in the array
24+
console.log(words.includes("C")); // true
25+
// There is UpperCase C in the array
26+
27+
28+
// 2nd Parameter is for to check from a specific index of array
29+
const test = [0, 1, 2, 3, 4, 5, 6, 7, 8];
30+
console.log(test.includes(4, 5)); // Checking 4 from 5th index // false
31+
console.log(test.includes(4, 1)); // Checking 4 from 1st index // true
32+
33+

0 commit comments

Comments
 (0)