underscore.js

Using underscore groupby to group an array of cars by their colour

社会主义新天地 提交于 2019-12-18 12:55:02
问题 I have an array of cars. car = { make: "nissan", model: "sunny", colour: "red" }; How would I use underscore.js to group the array by colour? I've tried a few combos but I'm not really sure how to specify my iterator condition: var carsGroupedByColor = _.groupBy(cars, false, colour); var carsGroupedByColor = _.groupBy(vars, false, function(cars){ return cars[colour]; }; They all return everything in the array each time. 回答1: You don't need the false second argument, the following will work:

Array Of JS Dates How To Group By Days

一曲冷凌霜 提交于 2019-12-18 12:48:11
问题 I'm trying to figure out the most optimal and with as minimum amount of loops way to group my array of js dates objects from this: (Take a note this is browser console output it's actully real JS dates like new Date()) [Sat Aug 08 2015 08:30:00 GMT+0200 (Central Europe Daylight Time), Sat Aug 08 2015 09:30:00 GMT+0200 (Central Europe Daylight Time), Sun Aug 09 2015 08:30:00 GMT+0200 (Central Europe Daylight Time), Sun Aug 09 2015 09:30:00 GMT+0200 (Central Europe Daylight Time), Mon Aug 10

Javascript - waiting for a number of asynchronous callbacks to return?

纵然是瞬间 提交于 2019-12-18 12:25:09
问题 What's the best way/library for handling multiple asynchronous callbacks? Right now, I have something like this: _.each(stuff, function(thing){ async(thing, callback); }); I need to execute some code after the callback has been fired for each element in stuff . What's the cleanest way to do this? I'm open to using libraries. 回答1: There is a great library called Async.js that helps solve problems like this with many async & flow control helpers. It provides several forEach functions that can

How can I get a unique array based on object property using underscore

自作多情 提交于 2019-12-18 12:03:58
问题 I have an array of objects and I want to get a new array from it that is unique based only on a single property, is there a simple way to achieve this? Eg. [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ] Would result in 2 objects with name = bill removed once. 回答1: Use the uniq function var destArray = _.uniq(sourceArray, function(x){ return x.name; }); From the docs: Produces a duplicate-free version of the array, using === to test object equality. If you know in

Remove empty properties / falsy values from Object with Underscore.js

北慕城南 提交于 2019-12-18 10:02:18
问题 I have an object with several properties. I would like to remove any properties that have falsy values. This can be achieved with compact on arrays, but what about objects? 回答1: You could make your own underscore plugin (mixin) : _.mixin({ compactObject: function(o) { _.each(o, function(v, k) { if(!v) { delete o[k]; } }); return o; } }); And then use it as a native underscore method : var o = _.compactObject({ foo: 'bar', a: 0, b: false, c: '', d: null, e: undefined }); Update As

Trouble combining Require.js and Backbone.js/Underscore.js

♀尐吖头ヾ 提交于 2019-12-18 09:23:29
问题 I've read previous threads where other members have had identical error messages as me, but their accepted solution doesn't seem to help. Basically I get this in Chrome console when I try to load index.html: Uncaught Error: Module name "underscore" has not been loaded yet for context: _. Use require([]) A couple of seconds later this shows up: Uncaught Error: Load timeout for modules: underscore,backbone Chrome network tools doesn't show any anomalies everything up until day_view.js is loaded

Finding nested duplicate arrays in JavaScript. (Nested Array uniq in lodash/underscore)

孤人 提交于 2019-12-18 09:08:45
问题 I am trying to determine if an array of JavaScript arrays contains duplicates. Is this possible? I am first trying to see if I can strip the duplicates out and then do an equality check but I cannot get past the first part. Here is what underscore returns: var arr1 = [[1,2], [2,3], [1,2]]; var arr2 = _.uniq(arr1); var arraysAreEqual = _.isEqual(arr1, arr2); console.log(arraysAreEqual, arr1, arr2); // true Jsbin: http://jsbin.com/vogumo/1/edit?js,console Anyone know of a way to determine if

underscore.js - Determine if all values in an array of arrays match

谁说胖子不能爱 提交于 2019-12-18 07:28:29
问题 I have an array of arrays, which looks something like this: [["Some string", "Some other string"],["Some third string", "some fourth string"]] I think I can use the _.all method in Underscore to determine if all of the arrays match 100% (that is all of their values match), but I'm not sure how to write the required iterator to run the check. Anyone have an idea? 回答1: Try this guy (order-independent): function allArraysAlike(arrays) { return _.all(arrays, function(array) { return array.length

Filtering object properties based on value

懵懂的女人 提交于 2019-12-18 04:01:08
问题 Is there some elegant way of filtering out falsey properties from this object with lodash/underscore? Similar to how _.compact(array) removes falsey elements from arrays so from { propA: true, propB: true, propC: false, propD: true, } returning { propA: true, propB: true, propD: true, } 回答1: Prior to Lodash 4.0 You want _.pick , it takes a function as an argument and returns an object only containing the keys for which that function returns truthy. So you can do: filtered = _.pick(obj,

Get an object from array which contains a specific value

落爺英雄遲暮 提交于 2019-12-18 01:30:31
问题 I am trying to search through an array of objects using Underscore.js, but I can't seem to target the one I want. console.log(_.findWhere(response.data, { TaskCategory: { TaskCategoryId: $routeParams.TaskCategory } })); However, this is returning undefined $routeParams.TaskCategory is equal to 301 This is an example of the objects inside the array I am searching. This data is represented by data.response [{ "TaskCategory": { "TaskCategoryId": 201, "TaskName": "TaskName" }, "TaskCount": 1,