underscore.js

Merge Javascript Objects and All References Pointing at Them

坚强是说给别人听的谎言 提交于 2019-12-24 04:15:06
问题 In javascript, variables referring to objects are passed a "copy of the reference". I am struggling with this concept as it relates to merging two objects, and the effect on the references to those objects. Attempting to use underscore's extend, take the following (coffeescript) code: a = { id: 1 type: "cat" name: "Angry" } b = { id: 1 type: "dog" owner: "Bill" } c = a d = c e = b _.extend(a, b) b = a d.home = "Nashville" console.log(a) //Object {id: 1, type: "dog", name: "Angry", owner:

Javascript merge arrays based on common element

核能气质少年 提交于 2019-12-24 01:53:19
问题 I'm trying to create a single object which takes information from two separate objects (taken from ajax calls). Basically we have a list of tags, and also a list of elements, these items are related, however the tag object doesn't contain all of the information I need to populate all the data. For example: List of tags: [ {"id": 1, "name": "yadda", "description": "yadda yadda"}, {"id": 2, "name": "yadda1", "description": "yadda yadda1"}, {"id": 7, "name": "yadda2", "description": "yadda

How can I merge javascript two objects but only update properties that change

不问归期 提交于 2019-12-24 00:56:32
问题 I'm trying to write a function that will let me update my kitchen in this example below. Using something like underscore's extend , however, blows away the beer in my fridge as it's updating the entire fridge object. Is there a simple method I can use so that I only make updates with the properties that change in my changeKitchen object and not update the entire fridge object? // My original kitchen var kitchen = { fridge: { beer: true, celery: false }, cabinets: { candy: true } }; // updates

Underscore debounce with arguments

你。 提交于 2019-12-23 14:54:25
问题 Suppose I have this event handler: var mousewheel = function (e) { /* blah */ }; But, I want to debounce it. So I do this, which works as expected: var mousewheelDebounced = _.debounce(mousewheel, 500); $(document).on("mousewheel", function (e) { e.preventDefault(); mousewheelDebounced(e); } But this doesn't work as expected : $(document).on("mousewheel", function (e) { e.preventDefault(); _.debounce(mousewheel, 500)(e); } I prefer the compactness of the second form. But no debouncing occurs.

Underscore debounce with arguments

扶醉桌前 提交于 2019-12-23 14:54:12
问题 Suppose I have this event handler: var mousewheel = function (e) { /* blah */ }; But, I want to debounce it. So I do this, which works as expected: var mousewheelDebounced = _.debounce(mousewheel, 500); $(document).on("mousewheel", function (e) { e.preventDefault(); mousewheelDebounced(e); } But this doesn't work as expected : $(document).on("mousewheel", function (e) { e.preventDefault(); _.debounce(mousewheel, 500)(e); } I prefer the compactness of the second form. But no debouncing occurs.

Why can't I create a random array using _.map(new Array(n), Math.random)?

爱⌒轻易说出口 提交于 2019-12-23 12:32:48
问题 I want to make an array of random numbers between 0 and 1, so I tried: var randList = _.map(new Array(5), Math.random); But instead of getting the list of random elements that I expected, I got: console.log(JSON.stringify(randList)); "[null,null,null,null,null]" Why did I get an Array of null instead of random numbers? 回答1: The Array(length) constructor creates a sparse array. That is, the length of the array is set to the specified parameter, but the elements of the array are not populated.

Backbone.js: After doing Fetch(), render only the new models

自古美人都是妖i 提交于 2019-12-23 10:10:37
问题 I have a Collection App.listingList where subsequent fetch() are called with add:true . App.listingList.fetch({ data: {some:data}, processData: true, add: true }); Problem: How can the newly added models have their views rendered, without re-rendering the views of the existing models. This means I cannot do: this.collection.each( function(listing, index) { new ListingMarkerView({ model:listing }).render(); }, this); Attempt #1 Rendering the View on the collection's add event, I cannot figure

Underscore.js, remove duplicates in array of objects based on key value

∥☆過路亽.° 提交于 2019-12-23 09:33:01
问题 I have the following JS array: var myArray = [{name:"Bob",b:"text2",c:true}, {name:"Tom",b:"text2",c:true}, {name:"Adam",b:"text2",c:true}, {name:"Tom",b:"text2",c:true}, {name:"Bob",b:"text2",c:true} ]; I want to eliminate the indexes with name value duplicates and recreate a new array, with distinct names, eg: var mySubArray = [{name:"Bob",b:"text2",c:true}, {name:"Tom",b:"text2",c:true}, {name:"Adam",b:"text2",c:true}, ]; As you can see, I removed "Bob" and "Tom", leaving only 3 distinct

How to compare an array to an array of arrays?

妖精的绣舞 提交于 2019-12-23 06:59:01
问题 This is an attempt in a tic tac toe game app. I have two arrays playerMoves and winningCombinations . Like this. var playerMoves= [0,1,4]; var winningCombinations = [ [0,1,2],[3,4,5],[6,7,8], [0,3,6],[1,4,7],[2,5,8], [0,4,8],[2,4,6] ]; I need to filter the winningCombination array such that at-least and at-most two values of playerMoves array matches with each array in winningCombination . findPossibleMove(playerMoves); // should return [[0,1,2],[1,4,7], [0,4,8] ] My attempt function

Ignoring undefined data / vars in an underscore template

别说谁变了你拦得住时间么 提交于 2019-12-23 06:52:10
问题 Still learning backbone so bear with me; I'm trying to add a new model with blank fields to a view, but the template I've created has a whole bunch of <input value="<%= some_value %>" type="whatever" /> Works perfectly fine when fetching data, it populates it and all goes well. The trouble arises when I want to create a new (blank) rendered view, it gives me Uncaught ReferenceError: some_value is not defined I can set defaults (I do already for a few that have default values in the db) but