underscore.js

How to use Underscore.js filter with an object?

大兔子大兔子 提交于 2019-12-22 04:09:44
问题 I have an object like so: > Object > Rett@site.com: Array[100] > pel4@gmail.com: Array[4] > 0 id : 132 selected : true > 1 id : 51 selected : false etc.. How can I use the underscore _.filter() to return back only the items where selected === true? I've never had the need to go down to layers with _.filter() . Something like var stuff = _.filter(me.collections, function(item) { return item[0].selected === true; }); Thank you 回答1: If you want to pull all array elements from any e-mail address

JavaScript error: “is not a constructor”

强颜欢笑 提交于 2019-12-22 03:09:29
问题 I'm using backbone.js along with jquery and underscore.js Here's some of my code (it doesn't do anything yet). The strange thing is that upon hitting the url "/#users" there isn't an error. The only time the error happens is when I then click to go to a different hash and then click back to go to "/#users". Here's part of my code, with the line that receives the error toward the end Users = new Users(); the error says "Users is not a constructor": var User = Backbone.Model.extend({ url:

Why does Underscore.js have a delay function?

女生的网名这么多〃 提交于 2019-12-22 02:59:23
问题 This is the source code for Underscore.js' delay function: _.delay = function (func, wait) { var args = slice.call(arguments, 2); return setTimeout(function () { return func.apply(null, args); }, wait); }; How is this any different from setTimeout ? Why does Underscore.js need delay ? 回答1: It's a cross browser way of being able to pass extra arguments which will appear as the arguments to the callback, like setTimeout() . This doesn't work in IE. It can make your code prettier... setTimeout(_

Underscore templating: Can't get switch to work

拥有回忆 提交于 2019-12-22 01:59:03
问题 I can't get a simple switch statement working in my underscore template. It's using the value of a variable called UserType which I've checked exists by displaying it with <%= UserType %>. Code coming up: <% switch(UserType) { %> <% case 13: %> <button id="schoolButton" value="schools" class="gridChooser k-textbox">Schools</button> <% case 12: %> <button id="teacherButton" value="teachers" class="gridChooser k-textbox">Teacher</button> <% case 8: %> <button id="classButton" value="classes"

Underscore templating: Can't get switch to work

我怕爱的太早我们不能终老 提交于 2019-12-22 01:56:09
问题 I can't get a simple switch statement working in my underscore template. It's using the value of a variable called UserType which I've checked exists by displaying it with <%= UserType %>. Code coming up: <% switch(UserType) { %> <% case 13: %> <button id="schoolButton" value="schools" class="gridChooser k-textbox">Schools</button> <% case 12: %> <button id="teacherButton" value="teachers" class="gridChooser k-textbox">Teacher</button> <% case 8: %> <button id="classButton" value="classes"

Get next key-value pair in an object

自作多情 提交于 2019-12-22 01:26:35
问题 Given a key, I want to find the next property in an object. I can not rely on the keys to be ordered or sequential (they're uuids). Please see below for trivial example of what I want: var db = { a: 1, b: 2, c: 3 } var next = function(db, key) { // ??? } next(db, 'a'); // I want 2 next(db, 'b'); // I want 3 I also want a prev() function, but I'm sure it will be the same solution. This seems like such a trivial problem but I can't for the life of me figure out how to do it. Happy for the

What does plus operator mean in Underscore.js?

妖精的绣舞 提交于 2019-12-22 01:02:31
问题 While I was seeing Underscore.js (version 1.4.3) code, I saw following lines (79 line) if (obj.length === +obj.length) { for (var i = 0, l = obj.length; i < l; i++) { if (iterator.call(context, obj[i], i, obj) === breaker) return; } } I didn't understand why + operator used within if statement. (+obj.length) And, isn't it this statement always true ? I don't think it's a typo. There must be some aim to use that. If anyone knows benefits of this usage, I want to use it in future. Thanks. 回答1:

Filter the original array of objects by array of ids

柔情痞子 提交于 2019-12-21 19:56:21
问题 I have two arrays: array a: var a = [ { id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' } ]; array ids: var ids = [1]; I want to array a filtered by array ids, result i wanted: var a = [ { id: 1, name: 'a' } ]; The most important thing is i want the change on the original array, rather than return a new array. underscore solution is better:) 回答1: You can use .filter a = a.filter(function(el){ return ~ids.indexOf(el.id) }); // should give you [{id: 1, name: 'a'}] 回答2: Today I

Underscore.js calling isNaN after isFinite function

ⅰ亾dé卋堺 提交于 2019-12-21 19:48:12
问题 In Underscore.js library, there is a function named isFinite returning 'true' if the value is a number. Considering that the built-in function isFinite of Javascript is already returning 'true' if the value passed as argument is a number, why we also need to call !isNaN(parseFloat(obj)) ? // Is a given object a finite number? _.isFinite = function(obj) { return isFinite(obj) && !isNaN(parseFloat(obj)); }; 回答1: This covers the case of isFinite("") , isFinite(null) and isFinite(false) all

How to solve a conflict between browserify (Backbone based app) and require.js on the same page?

半城伤御伤魂 提交于 2019-12-21 19:38:09
问题 I have a backbone app up and running correctly. It's meant to be used as a widget in 3rd pages. Unfortunately, I just realised that one of these pages has already Backbone / underscore loaded. I get errors such as this: Uncaught TypeError: Cannot read property 'extend' of undefined Which usually appear when underscore has not been previously loaded. My typical view is like this: (normal Backbone view) ./view1.js var Backbone = require('backbone') var _ = require('underscore') var $ = require(