underscore.js

How to get a value from specificed key

蹲街弑〆低调 提交于 2019-12-11 16:15:40
问题 var keys = ['name','age','gender']; var input = {"document": {"people":[ {"name":["Harry Potter"],"age":["18"],"gender":["Male"]}, {"name":["hermione granger"],"age":["18"],"gender":["Female"]}, ]} } How to get values from specific key. Example {Harry Potter, 18, Male} ? 回答1: You could use pick to extract the properties that you're interested in: var people = _.map(input.document.people, function(person){ return _.flatten( _.values(_.pick(person, keys))); }); angular.module('MyModule', [])

Adding more, another loop to my Underscore template with a new backbone model?

℡╲_俬逩灬. 提交于 2019-12-11 14:27:29
问题 UPDATE : I have got my code working, of sorts, but I have two issues and one problem I am not sure how to fix. I will post my current code below. One The Clients append to the right section within the TimesheetData template. But it wraps the option tag within the ClientData template within another option . So I get : <select> <option> <option value="XX"> XXX </option> </option> </select> Now I know this is what it is designed to do, to have a root element but I can not seem to find a solution

Merge objects with different values using Angularjs or Underscore js

我的未来我决定 提交于 2019-12-11 12:40:51
问题 I'm trying to merge two objects into a single multidimensional object for use in Angularjs controller by the 'unique_id'. (Note I also have Underscore Js added in). Object #1 example: [ { "unique_id": "001", "title": "Putting Green Challenge - Motion Depends on Force and Mass" }, { "unique_id": "002", "title": "Molecules to Organisms: Frog Life Cycle" } ] Object #2 example (has MANY more rows than object 1..): [ { "ab_id": "76153F02-29F3-11D8-95EA-951BF95D9AEF", "unique_id": "001", "title":

JavaScript group objects

家住魔仙堡 提交于 2019-12-11 12:08:39
问题 I have been looking for a few days now and I haven't found the answer to this specific problem. I'm receiving a javascript object array from an endpoint in my API. I need to group the objects together based on the Type. example hard-coded array of objects: $scope.content = { things: [ { thing: 'one', thingType: 'load' }, { thing: 'two', thingType: 'export' }, { thing: 'three', thingType: 'export' } ] } var typeArr = []; for (var key in $scope.content.things) { typeArr[key] = $scope.content

Outputting directly to template with Embedded Javascript Syntax

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 12:00:15
问题 I'm using Backbone with Underscore templates. I have a JavaScript if() condition in my code that looks something like this: <div class='faces'> <% if(somevalue === true) { %> your face <% } else { %> my face <% } %> </div> However I find this syntax awkward and I really would like to use something like the following, even though it doesn't actually work (replaces entire document with the text): <div class='faces'> <% if(somevalue === true) { document.write("your face"); } else { document

How can I reduce array with non-unique elements by summing their second value with lodash/underscore?

本小妞迷上赌 提交于 2019-12-11 11:54:33
问题 How to get from this array [ {name: "a", weight: 1}, {name: "b", weight: 3}, {name: "a", weight: 5}, {name: "b", weight: 7} ] get this array(weight of duplicates summed) [ {name: "a", weight: 6}, {name: "b", weight: 10} ] 回答1: Something like this should work using groupBy reduce and pluck : var sum = function(a,b){ return a+b; }; _.chain([ {name: "a", weight: 1}, {name: "b", weight: 3}, {name: "a", weight: 5}, {name: "b", weight: 7} ]).groupBy('name') // we first group them by name .map

how to get a model by array params from a collection

天大地大妈咪最大 提交于 2019-12-11 11:44:34
问题 I have a model like this: model = from: "a@b.com" id: 1 to: [c@d.com] and I have a collection containing these kind of models. The collection needs to filter by from . I know _.where an underscore.js function. I am using it like this: fromIds = _.pluck _.where(msgs, from : login), 'msgId' and need to filter by 'to' as well: toIds = _.pluck _.where(msgs, to : login), 'msgId' it does not work, because to is an array. How can I filter by to ? I would be grateful if someone helped me out ! 回答1:

Shorten an array or collection with an array of fields

时间秒杀一切 提交于 2019-12-11 11:35:18
问题 Scenario : you have an array, you have an array of some ids (eg: [1,3]), you want to get a shortened array. I cannot find how to do so if not in the lengthy way: _.filter(... function (id) { return id==1 || id==3 }) Question : does such someMethod1 exist in underscore.js? var frdsArr = [ {id: 1, name: 'moe', age: 40}, {id: 2, name: 'larry', age: 50}, {id: 3, name: 'curly', age: 60} ]; var goodFrdsArr = _.someMethod1( frdsArr, 'id', [1, 3] ); /* goodFrdsArr is [ {id: 1, name: 'moe', age: 40},

Filter unique & duplicate objects in array using underscore

扶醉桌前 提交于 2019-12-11 11:17:20
问题 I have an array of objects: data = [{"origin":"SJU","dest":"JFK","rank":48},{"origin":"JFK","dest":"SJU","rank":21},{"origin":"IAD","dest":"LAX","rank":31},{"origin":"LAS","dest":"SJU","rank":21}] I am trying to extract all duplicates & unique objects BY "origin" & "dest". So that these 2 are the same, ignoring the rank key {origin:'JFK',dest:'SJU',rank:21} {"origin":"SJU","dest":"JFK","rank":48} Basically I want 2 seperate arrays: duplicates=[{"origin":"SJU","dest":"JFK","rank":48},{"origin"

ng-show not working with UnderscoreJS's _.isNull but works with val === null

不想你离开。 提交于 2019-12-11 11:15:45
问题 I've got an element with an ng-show directive and when I use the UnderscoreJS helper function _.isNull it work the same was as using the standard === operator. So this displays the content with newJobWorkPercentage is null. <div class="form-group" ng-show="newJobWorkPercentage === null"> ... some content </div> but this doesn't <div class="form-group" ng-show="_.isNull(newJobWorkPercentage)"> ... some content </div> I can just use the === in my app, I'm just intrigued why the helper doesn't