underscore.js

How flatten object literal properties?

喜夏-厌秋 提交于 2020-01-06 16:18:12
问题 I have an object being returned by a legacy server and I want to change the structure on the client-side via JavaScript, jQuery, or even Underscore.js. Below is what my original object looks like: [ { "Id":{ "LValue":1, "Value":1 }, "Date":{ "LValue":"2013-10-17T00:00:00", "Value":"2013-10-24T00:00:00" }, "User":{ "LValue":508, "Value":507 }, "Comments":{ "LValue":"This a test load", "Value":"This a test" }, "Name":"John Doe", "IsDeleted":false } ] On the client-side though, I would like to

Merging array objects with same properies

笑着哭i 提交于 2020-01-06 06:04:49
问题 i am doing one of my front end project and i have a situation where i have to merge/add objects present in the array based on some conditions. Conditions would be Only those Objects having same label should be merged. For objects which has same label, if object 'a' has properties which are present in b object as well, the value has to be added, else simply copy the property. So my input would be [ { label: 'label-1', published: 1, draft: 2, id: 'some1' }, { label: 'label-1', published: 2,

Merge 2 objects based on 1 field using Underscore.js

放肆的年华 提交于 2020-01-05 05:54:13
问题 I have 2 arrays of objects: var a = [ { "image_id": 631293, "score": 73 }, { "image_id": 11848407, "score": 56 } ]; var b = [ { "image_id": 631293, "article_id": 173 }, { "image_id": 11848407, "article_id": 121 } ]; I have tried to apply method described in How can i merge array of objects with underscore js but unfortunately it does not work at all. My need is to merge both arrays of objects thanks to image_id . So I tried this : _.values( _.extendOwn( _.indexBy(a, 'image_id'), _.indexBy(b,

Deep picking using Underscore.JS

亡梦爱人 提交于 2020-01-05 05:25:07
问题 I am trying to use underscoreJs to manipulate a JavaScript object and having problems doing so. Here is my example var data = { "label": "SomeName", "parent": [{ "id": "parentId", "resources": [{ "name": "ID1NAME", "calls": [ "user_get", "user2_post", "user3_delete" ] }, { "name": "ID2", "calls": [ "employee1_get", "employee2_delete", "employee3_update" ] }] }] }; var res = _(data).chain(). pluck('parent'). flatten(). findWhere(function(item){ item === "user_get" }). value(); console.log(res)

Create a simpler way of nesting functions

百般思念 提交于 2020-01-04 17:37:29
问题 I'm looking to lower my overhead on code like this foo(bar(baz("hello"))) // function hell ideally something like this var fbb = bind(foo, bar, baz) foo("hello") Does this exist? Native or library? I looked through underscore and bind. 回答1: Underscore has the compose function which will do what you want: var composite = _.compose(foo, bar, baz); composite('hello'); function foo(a1){ return 'foo' + a1; } function bar(a2){ return 'bar' + a2; } function baz(a3){ return 'baz' + a3; } alert(foo

Why does UnderscoreJS have wrapper functions around a lot of native Javascript functions?

≯℡__Kan透↙ 提交于 2020-01-04 01:55:09
问题 I noticed that UnderScoreJS has a lot of wrapper functions around native Javascript functions. Take for example: _.isArray, _.isBoolean, _.isNaN? Is there any reason for this? Or are these simply meant for ensuring code consistency when using an underscoreJS library OR just enhancing these functions in anyway? For example, the _.isArray function gets down to this: _.isArray = nativeIsArray || function(obj) { return toString.call(obj) === '[object Array]'; }; Any idea? 回答1: It's because those

How to return value from debounced function in javascript? [duplicate]

大兔子大兔子 提交于 2020-01-03 16:14:19
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 3 years ago . I have a code like that: var originalFunction = function() { return 'some value'; }; var debouncedFunction = _.debounce(originalFunction, 3000); console.log('debouncedFunction() result: ', debouncedFunction()); console.log('originalFunction() result: ', originalFunction()); (codepen link) And the result in the console is: debouncedFunction() result: undefined

browserify source map not working if require underscore

岁酱吖の 提交于 2020-01-03 08:14:10
问题 I use browserify for my angular client app. I need to use underscore. angular is installed with bower and underscore is installed with npm This is how I run browserify (npm) and create source map in gulp (npm) gulp.task('browserify', function() { return browserify(dir.script_from + '/main.js', {debug: true}) .bundle() .pipe(source('bundle.js')) // gives streaming vinyl file object .pipe(buffer()) // <----- convert from streaming to buffered vinyl file object .pipe(sourcemaps.init({loadMaps:

Rails 4 - JS for dependent fields with simple form

谁说我不能喝 提交于 2020-01-03 07:17:34
问题 I am trying to make an app in Rails 4. I am using simple form for forms and have just tried to use gem 'dependent-fields-rails' to hide or show subset questions based on the form field of a primary question. I'm getting stuck. I have added gems to my gem file for: gem 'dependent-fields-rails' gem 'underscore-rails' I have updated my application.js to: //= require dependent-fields //= require underscore I have a form which has: <%= f.simple_fields_for :project_date do |pdf| %> <%= pdf.error

How do I create a collection of arrays from a single array?

旧巷老猫 提交于 2020-01-03 06:57:25
问题 Say I have one large array like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] and would like to split it into an array of n-tuples like [[1,2], [3,4], [5,6], [7,8], [9,10], [11,12], [13,14] /*, ... */ ] // (for n=2) Is there some easy way to achieve this? The special case n = 2 would be enough for me. 回答1: This should work: for (var i=0; i<arr.length; i+=2) { result.push([arr[i], arr[i+1]]); } Came up with this, it should work for any number of "pockets" or whatever you want