underscore.js

Lodash sort collection based on external array

安稳与你 提交于 2019-12-19 05:07:08
问题 I have an array with keys like so: ['asdf12','39342aa','12399','129asg',...] and a collection which has these keys in each object like so: [{guid: '39342aa', name: 'John'},{guid: '129asg', name: 'Mary'}, ... ] Is there a fast way to sort the collection based on the order of keys in the first array? 回答1: var sortedCollection = _.sortBy(collection, function(item){ return firstArray.indexOf(item.guid) }); 回答2: Input: var data1 = ['129asg', '39342aa']; var data2 = [{ guid: '39342aa', name: 'John'

Backbone: A list of views inside a view

[亡魂溺海] 提交于 2019-12-19 05:04:44
问题 Let me show what I need first so you can understand my problem. I have a contact view like: ContactView = Backbone.View.extend({ template: _.template("Name: <%= name %> E-mail: <%= email %> Phones <%= label1 %> - <%= number1 %> <%= label2 %> - <%= number2 %>"), render: function() { var contact = this.template(this.model.toJSON()); this.$el.html(contact); } }); So far so good, the problem is that the phones part is a list, my model holds an array of phones, and I need to put it dynamically in

Ionic infinite scroll

无人久伴 提交于 2019-12-19 04:09:29
问题 I am using wordpress as a backend for an app and I want to use infinite scroll but I am having trouble concatenating articles. I am calling the service using a factory: .factory('Worlds', function ($http) { var worlds = []; storageKey = "worlds"; function _getCache() { var cache = localStorage.getItem(storageKey ); if (cache) worlds = angular.fromJson(cache); } return { all: function () { return $http.get("http://www.examplesite.com/tna_wp/wp-json/posts?filter[category_name]=international

JavaScript : Make an array of value pairs form an array of values

岁酱吖の 提交于 2019-12-19 04:01:52
问题 Is there an elegant, functional way to turn this array: [ 1, 5, 9, 21 ] into this [ [1, 5], [5, 9], [9, 21] ] I know I could forEach the array and collect the values to create a new array. Is there an elegant way to do that in _.lodash without using a forEach ? 回答1: You could map a spliced array and check the index. If it is not zero, take the predecessor, otherwise the first element of the original array. var array = [1, 5, 9, 21], result = array.slice(1).map((a, i, aa) => [i ? aa[i - 1] :

flattening nested arrays/objects in underscore.js

穿精又带淫゛_ 提交于 2019-12-19 03:19:17
问题 I have an array of objects as shown below (although the example below has just one element in the array) [ { "uptime":0, "load":{"x":0.11,"y":0.22,"z":0.33}, "cpu":[ {"u":111,"n":112,"s":113,"i":114,"q":115}, {"u":211,"n":212,"s":213,"i":214,"q":215} ] } ] Im trying to flatten out each element using underscore.js, so the overall array looks like this: [ { "uptime":0, "load_x": 0.11 "load_y": 0.03 "load_z": 0.01, "cpu1_u": 111, "cpu1_n": 112, "cpu1_s": 113, "cpu1_i": 114, "cpu1_q": 115, "cpu2

return void 0; vs return; [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-18 20:32:26
问题 This question already has answers here : Return void(0); vs return; interrupting functions [duplicate] (2 answers) Closed 6 years ago . I'm studying the annotated source code of Underscore.js. http://underscorejs.org/docs/underscore.html#section-41 Here's the _.first method: _.first = _.head = _.take = function(array, n, guard) { if (array == null) return void 0; return (n == null) || guard ? array[0] : slice.call(array, 0, n); }; Question: Why 'return void 0;' and not just 'return;' ? As far

return void 0; vs return; [duplicate]

无人久伴 提交于 2019-12-18 20:31:08
问题 This question already has answers here : Return void(0); vs return; interrupting functions [duplicate] (2 answers) Closed 6 years ago . I'm studying the annotated source code of Underscore.js. http://underscorejs.org/docs/underscore.html#section-41 Here's the _.first method: _.first = _.head = _.take = function(array, n, guard) { if (array == null) return void 0; return (n == null) || guard ? array[0] : slice.call(array, 0, n); }; Question: Why 'return void 0;' and not just 'return;' ? As far

Filtering a Backbone Collection returns an array of Models

♀尐吖头ヾ 提交于 2019-12-18 18:38:22
问题 Sample Code: this.books = this.getBooksFromDatabase(); this.publishedBooks = this.books.filter(function(book) { return book.get("isPublished") === "1"; }); Here lies the problem: this.books.filter, returns an array of the models. I've tried wrapping the array, as such: var publishedBooks = _( this.books.filter(function(book) { return book.get("isPublished") === "1"; })) as recommended by this post: https://github.com/documentcloud/backbone/issues/120 But i still can't run things like:

Lodash Debounce not debouncing

二次信任 提交于 2019-12-18 14:15:24
问题 I'm trying to debounce a function using Lodash, and while it's invoking the function, it doesn't seem to debounce it at all. My issue doesn't seem to be the same mistake as what I've seen elsewhere on SO or Google (generally, that they're not invoking the function that _.debounce returns). My currently super-simple implementation is as follows (in Angular with CoffeeScript): s.search = -> _.debounce( s._makeSearchRequest, 1000 )() s._makeSearchRequest = -> console.log("making search request")

Using underscore.js to compare two Objects

会有一股神秘感。 提交于 2019-12-18 14:12:23
问题 I'm trying to compare two objects with underscore. Object 1 (Filter) { "tuxedoorsuit":"tuxedoorsuit-tuxedo", "occasions":"occasions-wedding" } Object 2 (Properties) { "tuxedoorsuit":"tuxedoorsuit-tuxedo", "occasions":"occasions-wedding", "occasions":"occasions-prom", "product_fit":"product_fit-slim", "colorfamily":"colorfamily-black" } I want to return true when all items of Object 1 are found within Object 2. What would be the best underscore method to use for this? 回答1: Edit: As per Arnaldo