lodash

Explicit chaining with lodash doesn't apply shortcut fusion

人走茶凉 提交于 2019-12-24 07:03:52
问题 While working with lodash, I found that it applied shortcut fusion when I used implicit chaining. $ node -e 'const _ = require("lodash"); _([1,2,3]).map(n => { console.log(n); return n }).find(n => n <= 1)' 1 But when I changed this snippet to use explicit chaining, it doesn't apply shortcut fusion. $ node -e 'const _ = require("lodash"); _.chain([1,2,3]).map(n => { console.log(n); return n }).find(n => n <= 1).value()' 1 2 3 As you can see, a function passed to map was called three times

Lodash mapping of nested collections

感情迁移 提交于 2019-12-24 06:29:48
问题 I have following collection: var realty = { name: 'Realty A', entrances: [ { name: 'Entrance A', units: [ {name: 'unitA', contracts: [{contractNo: 'no.963'}, {contractNo: 'no.741'}]}, {name: 'unitB', contracts: [{contractNo: 'no.789'}, {contractNo: 'no.564'}]} ] }, { name: 'Entrance B', units: [ {name: 'unitC', contracts: [{contractNo: 'no.419'}, {contractNo: 'no.748'}]}, {name: 'unitD', contracts: [{contractNo: 'no.951'}, {contractNo: 'no.357'}]} ] } ] } And I am trying to extract collection

Lodash mapping of nested collections

允我心安 提交于 2019-12-24 06:29:05
问题 I have following collection: var realty = { name: 'Realty A', entrances: [ { name: 'Entrance A', units: [ {name: 'unitA', contracts: [{contractNo: 'no.963'}, {contractNo: 'no.741'}]}, {name: 'unitB', contracts: [{contractNo: 'no.789'}, {contractNo: 'no.564'}]} ] }, { name: 'Entrance B', units: [ {name: 'unitC', contracts: [{contractNo: 'no.419'}, {contractNo: 'no.748'}]}, {name: 'unitD', contracts: [{contractNo: 'no.951'}, {contractNo: 'no.357'}]} ] } ] } And I am trying to extract collection

Whitelist a set of properties from a multidimensional json array and delete the rest

帅比萌擦擦* 提交于 2019-12-24 04:05:16
问题 For examples sake i'll use a github api response as my data input. https://api.github.com/users/unsalted/repos I have a list of properties I want to keep, the rest I want to discard because I want to keep the output i'm generating considerably more compact. How can I achieve my goal without doing this: for (var i = tagged.length - 1; i >= 0; i--) { delete tagged[i].private; delete tagged[i].owner.gravatar_id; delete tagged[i].owner.url; delete tagged[i].owner.followers_url; delete tagged[i]

webpack之旅: (五)基础打包Library库文件和TypeScript的webpack基础打包

拥有回忆 提交于 2019-12-24 03:05:20
今天开始我讲以一些具体的打包例子继续给大家说说webpack中一些可能比较常用到的配置项: 今天我将以两个简单的例子,打包Library文件和打包配置TypeScript的代码为例子为大家介绍: 关于Library库文件,我们可以将它理解成一些包文件,资源文件,就好比是npm上提供给我们使用的这些的包一样。既然如此,我们就应该清楚,这样的包文件都是可以被使用者直接以 import 这样的es module的语法或者 require 这样的commonjs的语法进行引入使用的,甚至有些可以被使用者直接在html文件中以 <script> 这样标签的形式直接被引入使用的,那么如此我们需要怎样对我们的文件进行相应的配置才是可以实现的呢? 它的实现其实很简单,我们只需要在output配置项中加入 libraryTarget 和 library 配置即可。关于这两个配置: libraryTarget: 该配置项可以配置为 "umd"值,表示我们当前打包的库文件允许使用任何的形式进行引入,包括es moduel 的引入和 commonjs 的引入等等。当然libraryTarget的配置参数也可以是其他的: 比如可以配置成 “this”, "window"之类的,当然这样配置后也就不支持es module和commonjs的引入方式了,所以这里我也就不具体阐述了。 library:

VUE中使用lodash的debounce和throttle方法

偶尔善良 提交于 2019-12-24 00:18:14
说明: debounce和throttle在脚手架的使用,此处以防抖函数debounce为例避免按钮被重复点击 引入: import lodash from 'lodash' 使用: 方法一:直接使用debounce方法 // 审核 audit : lodash . debounce ( function ( ) { this . $refs [ 'model' ] . saveTotalResult ( 1 ) . then ( ( ) => { const reportId = this . activeReport . id ; report . audit ( reportId ) . then ( res => { successMsg ( res . msg ) } ) } ) } , 300 ) , 方式二:方法内部调用,先声明后使用 audit ( ) { this . auditLoading = true ; // 需要使用箭头函数避免this指向问题(此处考虑按钮加载状态所以将loading放在debounce外部) const audit = lodash . debounce ( ( ) => { this . $refs [ 'model' ] . saveTotalResult ( 1 ) . then ( ( ) => { const

Get list of duplicate objects in an array of objects

拜拜、爱过 提交于 2019-12-24 00:14:02
问题 I am trying to get duplicate objects within an array of objects. Let's say the object is like below. values = [ { id: 10, name: 'someName1' }, { id: 10, name: 'someName2' }, { id: 11, name: 'someName3' }, { id: 12, name: 'someName4' } ]; Duplicate objects should return like below: duplicate = [ { id: 10, name: 'someName1' }, { id: 10, name: 'someName2' } ]; 回答1: You can use Array#reduce to make a counter lookup table based on the id key, then use Array#filter to remove any items that appeared

Lodash unexpectedly injects itself into global when required in subdependency

泄露秘密 提交于 2019-12-23 17:44:58
问题 My app depends on a library foolib , library foolib has a dependency on lodash and requires it via var _ = require('lodash') Requiring foolib results in lodash attaching itself to the window I found that this was due to this: https://github.com/lodash/lodash/blob/45785a282442399cfca829aea496104003f773e2/dist/lodash.js#L17041-L17046 // Some AMD build optimizers, like r.js, check for condition patterns like: if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) { //

Lodash get key values from array of objects

安稳与你 提交于 2019-12-23 17:07:31
问题 I have an array of objects , var out = [{ "type": "1", "from": "13052033555", "to": "4444444", "amount": "40000", "date": 1461575799, "status": "1" }, { "type": "2", "from": "13052033555", "to": "1111111", "amount": "30000", "date": 1461575884, "status": "1" }... ]; I get only it's values without keys Now i used this function to get the values from array like this, I pass array then it returns only values without keys function foo(a) { var values = []; for (var i = 0; i < a.length; i++) { var

AngularJs: Have Underscore/Lodash/_ in the view template

淺唱寂寞╮ 提交于 2019-12-23 09:57:34
问题 I am trying to have the Underscore/Lodash/_ available in the AngularJS view template. This way I can use _ like shown below: <li ng-repeat="number in _.range(100, 125)"><!-- Some logic here --></li> And for that matter, we can use any of those useful functions of Lodash. We can achieve this by just adding _ to the $scope of the controllers and directives as shown below: $scope._ = _; But I would like to have a one-time configuration/change that adds _ to every scope for every view templates.