-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.js
More file actions
25 lines (21 loc) · 873 Bytes
/
find.js
File metadata and controls
25 lines (21 loc) · 873 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
/* -------------------------------------------------------------------------- */
/* $Find */
/* ---------- retorna solo primer elemento que cumpla la condición ---------- */
/* -------------------------------------------------------------------------- */
/* ------- filter retorna todos los que cumplan y find solo el primero ------ */
const arrayObjects = [
{ name: "Jhon", age: 21 },
{ name: "Camila", age: 22 },
{ name: "Kevin", age: 15 },
];
// foreach
let foundFE = "";
arrayObjects.forEach((element, index) => {
if (element.name === "Camila") {
foundFE = arrayObjects[index];
}
});
console.log(foundFE); /* { name: "Camila", age: 22 } */
// filter
const found = arrayObjects.filter((element) => element.name === "Camila");
console.log(found); /* [{ name: "Camila", age: 22 }] */