lodash

I have two arrays, how do I find matchings elements and perform some action? (lodash)

孤街醉人 提交于 2019-12-10 10:08:02
问题 var array1 = [{Age: 24, Name: "Test", StudentID: 101, Checked: false}, {Age:25, Name: "Test", StudentID: 102, Checked: false}]; var array2 = [{ID: 101}]; If any element in array1 has a property of StudentID that is equal to an ID property present in array2 I'd like to set the Checked property in array1 to true. Any tips? I'd like to do this without writing nested _.each statements. This is my first take; however, I believe _.some performs an interation anyway. _.each($scope.array1, function

Should we use _.foreach() or better the native for of loop in TypeScript

青春壹個敷衍的年華 提交于 2019-12-10 04:30:18
问题 I just started to work in a new project working with TypeScript. I'm comming from another project that also worked with TypeScript. Since the native for of loop in TypeScript is avaible we decided (old project team) to use this one. Espacialy for me it was much more convenient to write the for of loop, relating to my java background. Now in the new project they use everywhere the _.foreach() loop to iterate over arrays. What I am wondering, is there a performance difference between the native

Lodash - '_' refers to a UMD global and lodash.js is not a module errors

怎甘沉沦 提交于 2019-12-10 03:34:41
问题 If I do nothing and add the type definition it works for jquery but lodash gets a '_' referes to a UMD global, but the current file is a module. Consider adding an import instead. If I try to import lodash with any combination of import call, I get lodash.js is not a module I tried to get help by asking a question here and it was closed because there was another answer for Angular (Angular 2). I really need a real answer so I'm re-posting with my own solutions, hoping someone will eventually

When mapping through an array check next item property

扶醉桌前 提交于 2019-12-10 02:14:15
问题 I'm currently mapping through an array i.e. contents.map((content) => { switch(content.type) { case: 1 console.log("type is one and next type is .."); case: 2 console.log("type is two") } }) And as you can see in case 1 I need to grab type of next item. I know that this is possible using for loop with i increment, but need to do it within a map. I'm open for suggestions using libraries like lodash (wasn't able to find anything in the documentation). 回答1: Array.prototype.map calls it's

Detecting if _ is lodash or underscore

跟風遠走 提交于 2019-12-10 01:54:44
问题 What is an "authoritative" way to detect if the _ variable is loaded with lodash or underscore? I am using lodash for a project in an environment where underscore may sometimes also be loaded. Currently, I've come up with this: /** * lodash defines a variable PLACEHOLDER = '__lodash_placeholder__' * so check if that is defined / contains the string "lodash" */ if ( typeof( _.PLACEHOLDER ) == 'undefined' || _.PLACEHOLDER.indexOf( 'lodash' ) < 0 ) { // _ is underscore, do what I need to access

Backbone / Underscore chain method with where method

旧时模样 提交于 2019-12-10 01:02:33
问题 There has to be something simple I am missing here. http://jsfiddle.net/v9mdZ/ I am just learning Backbone and Underscore/loDash and am trying to get familiar with chain . I have the following code, which works as expected: var ids = _.pluck(collection.where({'is_checked':true}), 'id'); I attempted to refactor this, using chain like so: var ids = collection.chain().where({'is_checked':true}).pluck('id').value(); Why doesn't the refactored code work? Am I using chain wrong? Solution (details

现代前端库开发指南系列(二):使用 webpack 构建一个库

匆匆过客 提交于 2019-12-09 22:02:11
前言 在前文中,我说过本系列文章的受众是在现代前端体系下能够熟练编写业务代码的同学,因此本文在介绍 webpack 配置时,仅提及构建一个库所特有的配置,其余配置请参考 webpack 官方文档。 输出产物 构建一个库与构建一个一般应用最大的不同点在于 构建完成后输出的产物 。 一般应用构建完成后会输出: 一个 html 文件 一个 js 入口 chunk 、若干子 chunk 若干 css 文件 若干其它资源,如图片、字体文件等 虽然输出的资源非常多,但实际上所有的依赖、加载关系都已经从 html 文件开始一层一层定下来了,换句话说,这个 html 文件实际上就是整个应用的入口。 一个库构建完成后会输出: 一个 CommonJS 格式的 js 文件 一个未压缩的 UMD 格式的 js 文件 一个已压缩的 UMD 格式的 js 文件 可能包括若干的 css 文件 可能包括若干的其它资源文件 库的入口分别是上面罗列的 js 文件;你可能会奇怪,一个库怎么会有3个入口文件呢?莫急,且听我一一道来。 CommonJS CommonJS 是 Node.js 推行的一种模块化规范,主要语法包括 module.exports 、 require() 等;而我们在使用 webpack 引入 npm 包时,实际上是处于 Node.js 环境,由此可知,这个 CommonJS 格式的入口 js 文件

use lodash to find substring from array of strings

隐身守侯 提交于 2019-12-09 15:53:57
问题 I'm learning lodash. Is it possible to use lodash to find a substring in an array of strings? var myArray = [ 'I like oranges and apples', 'I hate banana and grapes', 'I find mango ok', 'another array item about fruit' ] is it possible to confirm if the word 'oranges' is in my array? I've tried _.includes, _.some, _.indexOf but they all failed as they look at the full string, not a substring 回答1: You can easily construct an iteratee for some() using lodash's higher-order functions. For

Using Lodash debounce in React to prevent requesting data as long as the user is typing

寵の児 提交于 2019-12-09 13:23:04
问题 I don't want to fire requests as long as the user is typing. My code should throttle requests so that when the user types quickly, it will fire one request with the latest input value instead of many. For now when I'm typing "test" it fires 4 different requests: "t" "te" "tes" "test" So I found lodash _.debounce and _.throttle ( [https://lodash.com/docs/4.17.4#debounce] ) but don't really understand how I can inplement it to my code. Can anyone help me? My code: import React, { Component }

Lodash: Constructing single object from many - Merging/overriding properties

与世无争的帅哥 提交于 2019-12-09 04:21:39
问题 Note: I filed this question under lodash as I'm pretty sure it can help me solve that problem nicely, but haven't put my finger on it just now I have an object describing different user roles and their permissions; I will have something like 10-15 roles defined "like this" ( this doesn't reflect the application code but the problem itself ): var role1 = { views: { v1: {access: true}, v2: {access: false}, v#: {access: false} } } var role2 = { views: { v1: {access: false}, v2: {access: true},