× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Array of objects as a client-side collection in Meteor

Tomáš Hromník
Jun 10, 2015
<p>You might need to use client-side only array of objects, so you create something like this:</p> <pre>var persons = [ { id: 'a1', name: 'John', age: 23 }, { id: 'a2', name: 'Josh', age: 25 }, { id: 'a3', name: 'Tom', age: 26 } ];</pre> <p>Everything is OK untill you want to search in persons array by id, name or age. You can find existing functions around the internet for this, but we have better solution in Meteor. We can use local-only Meteor Collections.</p> <pre>//pass null as collection name, it will create //local only collection Persons = new Mongo.Collection(null); var persons = [ { id: 'a1', name: 'John', age: 23 }, { id: 'a2', name: 'Josh', age: 25 }, { id: 'a3', name: 'Tom', age: 26 } ]; for (var i = 0; i &lt; persons.length; i++) { Persons.insert(persons[i]); } var tom = Persons.findOne({ name: 'Tom' });</pre> <p>That's all. You can just use your existing local array and insert its values into local Persons collection. This solution takes advantage of Meteor collections implementation. I love it.</p>
comments powered by Disqus