underscore.js

Get the min and max from array of objects with underscore.js

て烟熏妆下的殇ゞ 提交于 2019-12-10 06:59:05
问题 Let's say I have the following structure var myArrofObjects = [ {prop1:"10", prop2:"20", prop3: "somevalue1"}, {prop1:"11", prop2:"26", prop3: "somevalue2"}, {prop1:"67", prop2:"78", prop3: "somevalue3"} ]; I need to find the min and max based on prop2, so here my numbers would be 20 and 78. How can I write code in Underscore to do that? 回答1: You don't really need underscore for something like this. Math.max(...arrayOfObjects.map(elt => elt.prop2)); If you're not an ES6 kind of guy, then Math

Backbone View Nesting

喜你入骨 提交于 2019-12-10 03:22:24
问题 I'm running around in circles seemingly missing something in my current app implementing backbone.js. The problem is I have a master AppView, which initializes various subviews (a graph, a table of information, etc) for the page. My desire is to be able to change the layout of the page depending on a parameter flag passed along while navigating. What I run into is a case where the subviews, which have reference to dom elements that would be in place after the template is rendered, do not have

why does _.escape modify / characters in Underscore.js?

 ̄綄美尐妖づ 提交于 2019-12-10 02:36:59
问题 I was looking through the Underscore.js api and I noticed that _.escape escapes & , < , > , " , ' , and / characters. What surprised me was escaping / . Is there a reason to escape / characters that I don't know about? 回答1: EDIT : Alright, apparently, it is recommended by OWASP as it "helps end a HTML entity". Escape the following characters with HTML entity encoding to prevent switching into any execution context, such as script, style, or event handlers. Using hex entities is recommended in

Detecting if _ is lodash or underscore

跟風遠走 提交于 2019-12-10 01:54:44
问题 What is an "authoritative" way to detect if the _ variable is loaded with lodash or underscore? I am using lodash for a project in an environment where underscore may sometimes also be loaded. Currently, I've come up with this: /** * lodash defines a variable PLACEHOLDER = '__lodash_placeholder__' * so check if that is defined / contains the string "lodash" */ if ( typeof( _.PLACEHOLDER ) == 'undefined' || _.PLACEHOLDER.indexOf( 'lodash' ) < 0 ) { // _ is underscore, do what I need to access

checking for undefined variable in underscore template

核能气质少年 提交于 2019-12-10 01:21:37
问题 I show a modal view of libraryPrep objects in my template like this: if (_.isUndefined(this.libraryPreps)) { this.$el.html(this.template({ })); } else { this.$el.html(this.template({ libraryPreps: this.libraryPreps.toJSON() })); } The else statement works when I have a libraryPreps object. In my template, I use it like this: <select id="libraryPreps" > <% if (!_.isUndefined(libraryPreps)) { %> <% _.each(libraryPreps, function (libraryPrep) { %> <option value="<%=libraryPrep.id%>"><%=

Clustering elements within shape

旧城冷巷雨未停 提交于 2019-12-10 01:16:26
问题 I've seen this solution which appears to take care of clustering elements within a "consistent" shape without overlaps, but what if the shape was more obscure, like the following: My first couple stabs at this seem to point to simplifying the shape to it's most basic form, then performing checks if the element is within the actual shape coordinates, but that seems like A LOT of potential calculations that I was hoping to simplify. Any thoughts would be extremely appreciated. Thanks! JS Fiddle

Backbone / Underscore chain method with where method

旧时模样 提交于 2019-12-10 01:02:33
问题 There has to be something simple I am missing here. http://jsfiddle.net/v9mdZ/ I am just learning Backbone and Underscore/loDash and am trying to get familiar with chain . I have the following code, which works as expected: var ids = _.pluck(collection.where({'is_checked':true}), 'id'); I attempted to refactor this, using chain like so: var ids = collection.chain().where({'is_checked':true}).pluck('id').value(); Why doesn't the refactored code work? Am I using chain wrong? Solution (details

Extract object attribute from list of objects in Javascript

非 Y 不嫁゛ 提交于 2019-12-09 19:23:55
问题 I have the following object that I'm receiving from an API: { '2012-12-12': [ { 'id': 1234, 'type': 'A' }, { 'id': 1235, 'type': 'A' }, { 'id': 1236, 'type': 'B' }, ], '2012-12-13': [ { 'id': 1237, 'type': 'A' }, { 'id': 1238, 'type': 'C' }, { 'id': 1239, 'type': 'B' }, ] } Then I want to have another variable named types of type Array that will hold every possible value of the type attribute of each one of the objects. In this case it would be: types = ['A', 'B', 'C'] I'm trying to have it

compare two arrays if keys match with underscore.js

时光怂恿深爱的人放手 提交于 2019-12-09 17:12:38
问题 I have an array with permissions from Facebook and an array of the permissions that the user shouldve given: window.FB.api('/me/permissions', function(perm){ if(perm){ var given_permissions = _.keys(perm['data'][0]; var needed_permissions = ["publish_stream", "email"]; //now check if given permissions contains needed permissions } } Now I want to compare if all the needed_permissions are in given_permissions , in an underscore savvy way (without looping two arrays myself and compare values).

Iterating objects with underscore.js

拟墨画扇 提交于 2019-12-09 15:52:13
问题 So, I am learning out backbone.js and are currently iterating over some models in a view with the below example. The first snippet works, when the other underscore.js-based one doesn't. Why? // 1: Working this.collection.each(function(model){ console.log(model.get("description")); }); // 2: Not working _.each(this.collection, function(model){ console.log(model.get("description")); }); What am I doing wrong, as I can't see it by myself? 回答1: this.collection is an instance while this.collection