Skip to content

Commit 3063bcf

Browse files
committed
find, findIndex, every, some
1 parent ecb6c69 commit 3063bcf

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

app.js

+37-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
1-
// Array.from and Array.of - NOT ON THE PROTOTYPE
1+
// find - gets the specific item
2+
// findIndex - gets the index of the item
3+
// every - every item in the array needs to meet condition
4+
// some - atleast one item
25

3-
// from - returns Array Object from any object with a length property or an iterable object
4-
// from turns array-like/ish into array - string,nodeList,Set
6+
const people = [
7+
{ id: 0, name: "peter" },
8+
{ id: 1, name: "susan" },
9+
{ id: 2, name: "anna" },
10+
];
511

6-
const paragraphs = document.querySelectorAll("p");
7-
const result = document.getElementById("result");
8-
const second = document.getElementById("second");
12+
/* To get Anna - But filter returns an array,so need index to access */
13+
console.log("=== filter ===");
14+
const anna = people.filter((person) => person.name === "anna");
15+
console.log("Filter Anna: ", anna);
16+
console.log("Filter Anna: Name:", anna[0].name);
917

10-
let text = Array.from(paragraphs);
11-
text = text
12-
.map((item) => {
13-
return `<span>${item.textContent}</span>`;
14-
})
15-
.join(" ");
16-
result.innerHTML = text;
18+
/* find - returns an item*/
19+
console.log("=== find ===");
20+
const searchAnna = people.find((person) => person.name === "anna");
21+
console.log("searchAnna: ", searchAnna);
22+
console.log("searchAnnaName: ", searchAnna.name);
1723

18-
const newText = Array.from(document.querySelectorAll("p"), (item) => {
19-
return `<span>${item.textContent}</span>`;
20-
}).join(" ");
21-
second.innerHTML = newText;
24+
/* findIndex - return the index of the item*/
25+
console.log("=== findIndex ===");
26+
const searchAnnaIndex = people.findIndex((person) => person.name === "anna");
27+
console.log("SearchAnnaIndex: ", searchAnnaIndex);
28+
console.log("SearchAnnaIndexSliced: ", people.slice(0, searchAnnaIndex));
29+
30+
const grades = ["A", "B", "A", "B", "C"];
31+
const goodGrades = ["A", "B", "A", "B"];
32+
33+
/* every - return true if all the items accept the search criteria*/
34+
console.log("=== every ===");
35+
const allGoodGrades = goodGrades.every((grade) => grade !== "C");
36+
console.log("allGoodGrades: ", allGoodGrades);
37+
38+
/* some - returns true if one item is found within search criteria*/
39+
console.log("=== some ===");
40+
const oneBadGrade = goodGrades.some((grade) => grade === "C");
41+
console.log("oneBadGrade: ", oneBadGrade);

index.html

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
</head>
1313
<body>
1414
<h1 style="text-align: center">ES6</h1>
15-
<p>john</p>
16-
<p>susan</p>
17-
<p>peter</p>
1815
<h2 id="result"></h2>
1916
<h1 id="second"></h1>
2017

0 commit comments

Comments
 (0)