underscore.js

Recursive/deep extend/assign in Underscore.js?

人盡茶涼 提交于 2019-12-17 07:42:08
问题 Is there any way to get Underscore.js extend function: Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments. ... to work recursively? Actually, query property in creditOperation is going to completely override the query property defined in baseOperation : var url = require('url') , _ = require('underscore'), , baseOperation = { host:

Recursive/deep extend/assign in Underscore.js?

落爺英雄遲暮 提交于 2019-12-17 07:41:59
问题 Is there any way to get Underscore.js extend function: Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments. ... to work recursively? Actually, query property in creditOperation is going to completely override the query property defined in baseOperation : var url = require('url') , _ = require('underscore'), , baseOperation = { host:

Underscore: sortBy() based on multiple attributes

ε祈祈猫儿з 提交于 2019-12-17 04:45:10
问题 I am trying to sort an array with objects based on multiple attributes. I.e if the first attribute is the same between two objects a second attribute should be used to comapare the two objects. For example, consider the following array: var patients = [ [{name: 'John', roomNumber: 1, bedNumber: 1}], [{name: 'Lisa', roomNumber: 1, bedNumber: 2}], [{name: 'Chris', roomNumber: 2, bedNumber: 1}], [{name: 'Omar', roomNumber: 3, bedNumber: 1}] ]; Sorting these by the roomNumber attribute i would

External template in Underscore

拈花ヽ惹草 提交于 2019-12-17 02:52:40
问题 I use Underscore template. It is possible to attach a external file as template ? In Backbone View I have: textTemplate: _.template( $('#practice-text-template').html() ), initialize: function(){ this.words = new WordList; this.index = 0; this.render(); }, In my html is: <script id="practice-text-template" type="text/template"> <h3>something code</h3> </script> It works well. But I need external template . I try: <script id="practice-text-template" type="text/template" src="templates/tmp.js">

How to use underscore.js as a template engine?

我怕爱的太早我们不能终老 提交于 2019-12-16 22:09:24
问题 I'm trying to learn about new usages of javascript as a serverside language and as a functional language. Few days ago I heard about node.js and express framework. Then I saw about underscore.js as a set of utility functions. I saw this question on stackoverflow . It says we can use underscore.js as a template engine. anybody know good tutorials about how to use underscore.js for templating, especially for biginners who have less experience with advanced javascript. Thanks 回答1: Everything you

Find by key deep in a nested object

和自甴很熟 提交于 2019-12-16 20:33:23
问题 Let's say I have an object: [ { 'title': "some title" 'channel_id':'123we' 'options': [ { 'channel_id':'abc' 'image':'http://asdasd.com/all-inclusive-block-img.jpg' 'title':'All-Inclusive' 'options':[ { 'channel_id':'dsa2' 'title':'Some Recommends' 'options':[ { 'image':'http://www.asdasd.com' 'title':'Sandals' 'id':'1' 'content':{ ... I want to find the one object where the id is 1. Is there a function for something like this? I could use Underscore's _.filter method, but I would have to

JavaScript merging objects by id

我怕爱的太早我们不能终老 提交于 2019-12-16 20:01:16
问题 What's the correct way to merge two arrays in Javascript? I've got two arrays (for example): var a1 = [{ id : 1, name : "test"}, { id : 2, name : "test2"}] var a2 = [{ id : 1, count : "1"}, {id : 2, count : "2"}] I want to be able to end up with something like: var a3 = [{ id : 1, name : "test", count : "1"}, { id : 2, name : "test2", count : "2"}] Where the two arrays are being joined based on the 'id' field and extra data is simply being added. I tried to use _.union to do this, but it

How to improve this function to test Underscore Sample `_.sample()`

一笑奈何 提交于 2019-12-14 03:35:08
问题 The setup: I am using _.sample() for my first time in some client's code and I wanted to test it to make sure that it produced an even distribution of samples time after time. To test this, I created the following code: (function(arraySize, timesToRun){ arraySize = arraySize || 10; timesToRun = timesToRun || 1000; let myArray = Array.apply(null, {length: arraySize}).map(Number.call, Number); let resultsArray = Array.apply(null, Array(arraySize)).map(Number.prototype.valueOf,0); for (let i = 0

Rendering an Array as a List within a Backbone model

夙愿已清 提交于 2019-12-14 02:36:54
问题 This is an extension to another post I found: Backbone: A list of views inside a view I had trouble understanding this (I've never used underscore before) so I figured it's best to post a new topic about this. Scenario I have a model of a group chat which is created and lists a variety of information. This includes a list of the participants in that group chat. I need to display that list as part of the model being rendered. Code Creating the participants array var participants = []; $(iq)

How to group a collection of data by its keys using Underscore.js?

人走茶凉 提交于 2019-12-13 22:07:32
问题 Given the following data format, how can the data be grouped by their keys? The format of the data is: data=[ { "billingid": 138, "amount": "800", "billamount": "200", "balance": "1", "paid": "810", "billdate": "Apr 2, 2014 12:00:00 AM", "tax": "11", "patientid": "TMCH17", "configid": 6, "paymenttype": "cash", "servicename": "Room", "firstname": "kannan" }, { "billingid": 138, "amount": "800", "billamount": "500", "balance": "1", "paid": "810", "billdate": "Apr 2, 2014 12:00:00 AM", "tax":