Skip to content

Commit da566a5

Browse files
committed
Merge pull request #79 from GuillaumeBiton/where
new template for arrays of objects (collections?)
2 parents 6f70518 + 0b3e4cd commit da566a5

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Diff for: chapters/arrays/where-for-arrays-of-objects.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
layout: recipe
3+
title: where for arrays of objects
4+
chapter: Arrays
5+
---
6+
## Problem
7+
8+
You want to get an array of objects that match your request for some properties
9+
10+
You have an Array of Objects, such as:
11+
12+
{% highlight coffeescript %}
13+
cats = [
14+
{
15+
name: "Bubbles"
16+
favoriteFood: "mice"
17+
age: 1
18+
},
19+
{
20+
name: "Sparkle"
21+
favoriteFood: "tuna"
22+
},
23+
{
24+
name: "flyingCat"
25+
favoriteFood: "mice"
26+
age: 1
27+
}
28+
]
29+
{% endhighlight %}
30+
31+
You want to filter with some properties, like cats.where({ age: 1}) or cats.where({ age: 1, favoriteFood: "mice"})
32+
33+
## Solution
34+
35+
You can extend Array like this :
36+
37+
{% highlight coffeescript %}
38+
Array::where = (query) ->
39+
return [] if typeof query isnt "object"
40+
hit = Object.keys(query).length
41+
@filter (item) ->
42+
match = 0
43+
for key, val of query
44+
match += 1 if item[key] is val
45+
if match is hit then true else false
46+
47+
cats.where age:1
48+
# => [ { name: 'Bubbles', favoriteFood: 'mice', age: 1 },{ name: 'flyingCat', favoriteFood: 'mice', age: 1 } ]
49+
50+
cats.where age:1, name: "Bubbles"
51+
# => [ { name: 'Bubbles', favoriteFood: 'mice', age: 1 } ]
52+
53+
cats.where age:1, favoriteFood:"tuna"
54+
# => []
55+
{% endhighlight %}
56+
57+
## Discussion
58+
59+
This is an exact match. we could make it more flexible with a matcher function :
60+
61+
{% highlight coffeescript %}
62+
Array::where = (query, matcher = (a,b) -> a is b) ->
63+
return [] if typeof query isnt "object"
64+
hit = Object.keys(query).length
65+
@filter (item) ->
66+
match = 0
67+
for key, val of query
68+
match += 1 if matcher(item[key], val)
69+
if match is hit then true else false
70+
71+
cats.where name:"bubbles"
72+
# => []
73+
# it's case sensitive
74+
75+
cats.where name:"bubbles", (a, b) -> "#{ a }".toLowerCase() is "#{ b }".toLowerCase()
76+
# => [ { name: 'Bubbles', favoriteFood: 'mice', age: 1 } ]
77+
# now it's case insensitive
78+
{% endhighlight %}
79+
80+
it's more a method to deal with collection and it could be rename as "find" but popular libraires like underscore or lodash name it "where".

0 commit comments

Comments
 (0)