underscore.js

Loading templates with backbone js

流过昼夜 提交于 2020-01-12 14:01:33
问题 I'm starting in javascript development, and did a simple project with node.js as a rest API and a client using backbone, everything look perfectly till I want to get my templates out of my js. I found different approaches, some of them with some time (like one year old) but I can't understand which one could be better: A .js file with a var with the html code pros -> easy to load, easy to pass to underscore to compile it. cons -> scape every single line. app.templates.view = " \ <h3>something

Loading templates with backbone js

跟風遠走 提交于 2020-01-12 14:00:12
问题 I'm starting in javascript development, and did a simple project with node.js as a rest API and a client using backbone, everything look perfectly till I want to get my templates out of my js. I found different approaches, some of them with some time (like one year old) but I can't understand which one could be better: A .js file with a var with the html code pros -> easy to load, easy to pass to underscore to compile it. cons -> scape every single line. app.templates.view = " \ <h3>something

How can I find the index of an object inside a Array using underscore.js?

情到浓时终转凉″ 提交于 2020-01-12 07:18:22
问题 I want to get the index of the given value inside a Array using underscore.js. Here is my case var array = [{'id': 1, 'name': 'xxx'}, {'id': 2, 'name': 'yyy'}, {'id': 3, 'name': 'zzz'}]; var searchValue = {'id': 1, 'name': 'xxx'}; I used the following code, var index = _.indexOf(array, function(data) { alert(data.toSource()); //For testing purpose return data === searchValue; }); Also tried this too var index = _.indexOf(array, {id: searchValue.id}); But it returns -1 . Since it does not

Find number of occurences of string elements in an Array using lodash or underscore js

不想你离开。 提交于 2020-01-12 05:29:07
问题 I have an array in the following format: var array = [ { id: '555weibo' }, { id: '578weibo' }, { id: '111facebook' }, { id: '123facebook' }, { id: '145facebookpage' }, { id: '145facebookpage' }, { id: '766facebook' }, { id: '242facebook' }, { id: '432twitter' }, { id: '432exing' } ]; I need to find the number of occurrences of facebook , twitter , xing , and weibo inside that array. eg: { weibo: 2, facebook: 6, twitter: 1, xing: 1 } I've searched for a solution, but nothing seems to work. The

Remove entry from array using another array

拜拜、爱过 提交于 2020-01-11 11:29:13
问题 Not sure how do to this, so any help is greatly appreciated Say I have : const array1 = [1, 1, 2, 3, 4]; const array2 = [1, 2]; Desired output const result = [1, 3, 4]; I wish to compare array1 and array2 and for each entry in array2 , remove the equivalent from array1 . So if I have 3 of 1 in array1 and 1 of 1 in array2 , the resulting array should have 2 of 1. Working on a project that has both jquery and underscore.js if that makes anything easier. 回答1: var array1 = [1, 1, 2, 3, 4], array2

Remove empty Objects from Array

大憨熊 提交于 2020-01-11 10:51:11
问题 I have a JavaScript-array with objects filled in and want to remove every object with no data. It might look like this: var myArray = [ {id: "28b", text:"Phill"}, {id: "12c", text:"Peter"}, {id: "43f", text:"Ashley"}, {id: "43f", text:"Ashley"}, {id: "", text:""}, {id: "9a", text:"James"}, {id: "", text:""}, {id: "28b", text:"Phill"} ]; I already use _.uniq from underscore.js to remove all duplicates from my array, which works fine. Though they are unique, one empty Object is always left when

Underscore rendering [object HTMLDivElement]

五迷三道 提交于 2020-01-11 07:39:19
问题 I have an underscore template that is appending the following text " [object HTMLDivElement] " , but it's supposed to append value that "model.get('title')" returns. Here's my template: <script type="text/template" id="todoTemplate"> <div class='todoBlock'> <li class='appendedTodo'> <%= title %> </li> <button class='delete'>Delete</button><p> </div> </script> Here's my function: addTodoLi: function(model){ var todoData = model.get('title'); var compileTemplate = _.template( $('#todoTemplate')

Is there an indexOf in javascript to search an array with custom compare function

南楼画角 提交于 2020-01-09 07:12:09
问题 I need the index of the first value in the array, that matches a custom compare function. The very nice underscorej has a "find" function that returns the first value where a function returns true, but I would need this that returns the index instead. Is there a version of indexOf available somewhere, where I can pass a function used to comparing? Thanks for any suggestions! 来源: https://stackoverflow.com/questions/12356642/is-there-an-indexof-in-javascript-to-search-an-array-with-custom

obj.length === +obj.length in javascript

我怕爱的太早我们不能终老 提交于 2020-01-09 04:17:30
问题 In underscore.js source in many places I came across if (obj.length === +obj.length) Can someone explain, why do they use it? 回答1: It's another way of writing if (typeof obj.length == 'number') . Why they do it that way, it's anyone's guess. Probably trying to be clever at the expense of readability. Which is not too uncommon these days, unfortunately... Although it might be so that it can be compressed more by minifiers (YUI Compressor, Closure Compiler, UglifyJS, etc): (a.length===+a.length

Find average value for array of hashes using multiple group by

大城市里の小女人 提交于 2020-01-06 20:18:06
问题 based on Summarize array of objects and calculate average value for each unique object name I could partially solve my problem. I did group by ed_id but I need to group by ed_id and then el_id and to get average value based on those group by, so here is my array of hashes: a = [ { ed_id: 1, el_id: 127, value: 2 }, { ed_id: 1, el_id: 127, value: 6 }, { ed_id: 1, el_id: 129, value: 3 }, { ed_id: 1, el_id: 129, value: 13 }, { ed_id: 2, el_id: 127, value: 5 }, { ed_id: 2, el_id: 127, value: 9 },