lodash

lodash orderby with null and real values not ordering correctly

一个人想着一个人 提交于 2019-12-19 05:18:56
问题 I have an Angular 2 typescript application that is using lodash for various things. I have an array of objects that I am ordering using a property in the object... _.orderBy(this.myArray, ['propertyName'], ['desc']); This works well however my problem is that sometimes 'propertyName' can have a null value. These are ordered as the first item in a descending list, the highest real values then follow. I want to make these null values appear last in the descending ordering. I understand why the

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'

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] :

初识webpack 4

不羁的心 提交于 2019-12-19 01:19:15
网上大多数是webpack 3的教程,这是自己按照webpack官网最新版,webpack 4 的简单构建过程,在此记录一下 webpack安装 首先要安装node,并选择最新的长期支持版本(LTS - Long Term Support) nodejs官网下载 全局安装 npm install -g webpack 安装到项目目录,推荐安装到本项目 npm install --save-dev webpack 正式使用webpack前的准备 1、在终端中使用npm init命令可以自动创建这个package.json文件 npm init 输入这个命令后,终端会问你一系列诸如项目名称,项目描述,作者等信息,不过不用担心,如果你不准备在npm中发布你的模块,这些问题的答案都不重要,回车默认即可。 这时,在项目文件夹中,package.json文件已经就绪 2、安装Webpack,在本项目中安装webpack作为依赖 //如果是webpack 4 版本,需要安装webpack-cli npm install webpack webpack-cli --save-dev //先安装webpack,然后安装webpack-cli 3、创建项目目录 安装完后在原来的空文件夹中包括了packjson.json文件和node_modules文件夹,现在要创建新的内容,使其目录结构如下:

How to remove undefined values from array but keep 0 and null

天涯浪子 提交于 2019-12-18 18:33:11
问题 In javascript, I want to remove undefined values, but keep the values 0 and null from an array. [ 1, 2, 3, undefined, 0, null ] How can I do it cleanly? 回答1: You can use _.compact(array); Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey. See: https://lodash.com/docs/4.15.0#compact 回答2: The best way using lodash is _.without Example: const newArray = _.without([1,2,3,undefined,0,null], undefined); 回答3: No need for libraries with

Lodash union of arrays of objects

╄→гoц情女王★ 提交于 2019-12-18 14:48:22
问题 I'd like to use the _.union function to create a union of two arrays of objects. Union works with arrays of primitives only as it uses === to examine if two values are equal. I'd like to compare objects using a key property: objects with the same key property would be regarded equal. Is there a nice functional way to achieve that ideally using lodash? 回答1: A non pure lodash way to do this but using the array.concat function you are able to do this pretty simply along uniq() : var objUnion =

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")

Can _lodash test an array to check if an array element has a field with a certain value?

烈酒焚心 提交于 2019-12-18 12:46:12
问题 I have a variable selectedSubTopicId and I have an array of subTopic objects: objectiveDetail.subTopics[] . Each subTopic object has a field subTopicId I would like to use this to enable or disable and Add topic button. Can I use lodash in the ng-disabled to test this array and report true if any subTopic object element of the array has a subTopicId that is equal to the selectedSubTopicId . Here's a sample of the data that's in objectiveDetail. In this case there's just one element in the

How do you chain functions using lodash?

送分小仙女□ 提交于 2019-12-18 10:59:22
问题 I have an object that looks like var foundUser = { charData: [] } which then I load an object from a database using mysql and then I call _.assignIn(foundUser, rows[0]); But I get a few extra properties that I don't need (this isn't solvable by using select) So I call foundUser = _.omit(foundUser, ['blah']); But it would be nice if I could just do _.assignIn(foundUser, rows[0]).omit(rows[0], ['blah']); But that throws an error, am I doing it wrong or is there another way this can be done? 回答1

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