Skip to content

Commit b16a907

Browse files
new template for arrays of objects (collections?)
1 parent a10e302 commit b16a907

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

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

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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", (a, b) -> "#{ a }".toLowerCase() is "#{ b }".toLowerCase()
72+
# => [ { name: 'Bubbles', favoriteFood: 'mice', age: 1 } ]
73+
# now it's case insensitive
74+
{% endhighlight %}
75+
76+
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)