-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.js
50 lines (38 loc) · 842 Bytes
/
find.js
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
/*jshint esversion: 6 */
// Find //
// Search through array and look for
// a perticular element. Only returns the
// FIRST true element
var users = [
{name: "Alex"},
{name: "Matti"},
{name: "Bill"},
];
var user = users.find(function(user) {
return user.name === "Alex";
});
console.log(user);
function Car(model) {
this.model = model;
}
var cars = [
new Car("Volvo"),
new Car("Toyota"),
new Car("Ford"),
];
var car = cars.find(function(car) {
return car.model === "Volvo";
});
console.log(car);
var ladders = [
{ id: 1, height: 20 },
{ id: 3, height: 25 }
];
function findWhere(array, criteria) {
var proptery = Object.keys(criteria);
return array.find(function(item) {
return item[proptery] === criteria[proptery];
});
}
var products = findWhere(ladders, { height: 20 });
console.log(products);