-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (53 loc) · 1.45 KB
/
index.js
File metadata and controls
62 lines (53 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import data from "./data.js";
let filteredArray = new Array();
const cards = document.getElementById("cards");
const filterButton = document.getElementById("filterButton");
const filterInput = document.getElementById("filterInput");
const resetButton = document.getElementById("resetButton");
filterButton.addEventListener("click", (e) => {
getResults();
});
resetButton.addEventListener("click", (e) => {
restoreContent();
});
function createElements(arr) {
let output = new Array();
for (let i = 0; i < arr.length; i++) {
output += `<div>
<h3>${arr[i].name}</h3>
<p>Company: ${arr[i].company}</p>
<p>Email: ${arr[i].email}</p>
<p>Phone: ${arr[i].phone}</p>
<p>Age: ${arr[i].age}</p>
<p>Gender: ${arr[i].gender}</p>
<p>Eye color: ${arr[i].eyeColor}</p>
</div>`;
cards.innerHTML = output;
}
}
createElements(data);
function getResults() {
filterWith(data, filterInput.value);
while (cards.firstChild) {
cards.firstChild.remove();
}
createElements(filteredArray);
}
function restoreContent() {
while (cards.firstChild) {
cards.firstChild.remove();
}
createElements(data);
}
function filterWith(arr, filter) {
if (filter.length > 2) {
return (filteredArray = arr.filter((val) => {
return Object.values(val)
.toString()
.toLowerCase()
.includes(filter.toLowerCase());
}));
} else {
return [];
}
}