-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilterByChoice.js
More file actions
43 lines (39 loc) · 936 Bytes
/
filterByChoice.js
File metadata and controls
43 lines (39 loc) · 936 Bytes
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
const filterObject = (arr, aggVal) => {
if (typeof aggVal === "number") {
return arr.filter((_, index) => {
return index === aggVal;
});
}
return arr.filter((value) => {
const arrValue = Object.values(value);
for (val of arrValue) {
if (val === aggVal) {
return true;
}
}
return false;
});
};
const modifiedFliter = (arr, aggVal) => {
if (typeof aggVal === "number") {
return arr.filter((_, index) => {
return index === aggVal;
});
}
for (const entry of arr) {
for (const [key, value] of Object.entries(entry)) {
if (value === aggVal) {
return entry;
}
}
}
};
const arr = [
{ name: "Amir", id: "1" },
{ name: "Samlan", id: "2" },
{ name: "Shahrukh", id: "0" },
];
console.log(filterObject(arr, 0));
console.log(modifiedFliter(arr, "Amir"));
console.log(modifiedFliter(arr, "0"));
console.log(filterObject(arr, "-1"));