|
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 |
2 | 5 |
|
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 | +]; |
5 | 11 |
|
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); |
9 | 17 |
|
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); |
17 | 23 |
|
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); |
0 commit comments