Backbone.js How to iterate collection in descending order - backbone.js

I have a collection and I want to loop through it but starting from the last index to the first. Is it possible? How?

You can do the following:
bc.chain().reverse().each(function(item){console.log(item)});
That will 'chain' the collection, reverse it and iterated over the reversed version.
Hope that helps.

Related

How to compile a list of other lists by checkbox?

I'm trying to write a cell formula which can essentially create a single playlist of songs.
Currently the songs are grouped by decade, but I'd like to be able to see a single list of everything that has been ticked.
I tried an array formula, but it only returned the first ticked song. Plus not sure how to make the array formula include the adjacent lists.
I tried a FILTER function, it works for one list of songs, but I don't know how to get it to append the other lists on the end.
Could I use a QUERY function? Not sure how though.
Many thanks!
try:
={"LIST"; FILTER({C:C; F:F}; {B:B; E:E}=TRUE)}
awesome question! You were super close in your filter example, one more filter in your array would've done it :)
Example Image:
Example Formula:
={"LIST"; FILTER(C:C, B:B=TRUE); FILTER(F:F, E:E=TRUE)}

filtering for max value in collection in backbone.js

I have a collection of models having structure:
{"name":"xyz","company":"abc","recNumber":"M34/14-15/23"}
I need to get that specific model from this collection whose recNumber is highest(based on number after last slash). I guess it can be done with .filter from underscore but don't know what will be the exact construct in this case.
try this;
model = collection.max(function(m){
return _.last(m.get('recNumber').split('/'));
});
collection.max() Returns the maximum value in list.
_.last(array) Returns the last element of an array.
jsfiddle here

Array Display Order

a=Array.new
a=[1,2,3,4,5]
puts a
then output will be 1,2,3,4,5, but I want to display a[1] to a[0]
(2,3,4,5,1).
Are there any built-in methods for display in the above order?
Thanks in advance.
I'm afraid there's no built in method to display it in the order you want - you would have to customize/implement your own toString (or similar) method/utility to achieve an arbitrary display of array elements

Backbone collection: Retrieve distinct values of a collection

I have backbone collection of models and would like to retrieve the distinct values of a certain property
If I have loaded data like the following into my collection:
[{brand:'audi',id:'1234'},
{brand:'audi',id:'3456'},
{brand:'bmw',id:'3456'}]
I would now want to get the distinct brands from the collection. The result should be:
['audi','bmw']
Looking at the Backbone and Underscore API I don't see anything obvious to achieve this.
My current approach would be to use a library like jslinq
Am I missing anything obvious or does somebody have a better suggestion than jslinq?
You can use pluck and then use uniq (example: http://jsfiddle.net/sCVyN/5/)
pluck
A convenient version of what is perhaps the most common use-case for
map: extracting a list of property values.
uniq
Produces a duplicate-free version of the array, using === to test
object equality. If you know in advance that the array is sorted,
passing true for isSorted will run a much faster algorithm. If you
want to compute unique items based on a transformation, pass an
iterator function.

Pymongo iteratively safe saving and counting

I'm trying to pick out unique elements from a collection of objects and then save them to a sub collection.
The code I have is:
for item in db.col1.find({'Summary': {'$ne':{}}}):
current_specs = item['Summary']['Specs']
if not db.col1.specs.find({'Specs':current_specs}).count():
db.col1.specs.save({'Specs':current_specs, 'Updated': datetime.datetime.now()},safe=True)
This produces duplicate entries within db.col1.specs. I thought by using safe=True, it would make sure the write was completed and hence duplicates would not be added but this does not seem to be the case.
Can anyone explain why this is failing and the correct way of doing it?
Cheers
Figured it out:
the item['Summary']['Specs'] entry is itself a dictionary and to search for dictionaries you have to search for each individual dictionary entry
e.g. if the object looks like:
current_specs = {'field1': something, 'field2': something_else}
then we can find it via:
find({'Specs.field1':current_specs[field1], 'Specs.field2': current_specs[field2]})
you can't just use:
find({'Specs':current_specs})

Resources