lodash

lodash sortBy then groupBy, is order maintained?

♀尐吖头ヾ 提交于 2019-12-03 13:02:55
I'm having trouble figuring out from the lodash documentation if my assumption about sorting and grouping is correct. If I use sortBy, then use groupBy, do the arrays produced by groupBy maintain the sort order of items? For example, say I have the following array: var testArray = [[5,6],[1,3],[5,4],[5,1]] And I would like to group these by their first element, but also have them sorted by their second element within these groups. So, in lodash I assume I can do the following: _.chain(testArray) .sortBy(function (item) { return item[1]; }) .groupBy(function (item) { return item[0]; }) .value()

Lodash / javascript : Compare two collections and return the differences [duplicate]

最后都变了- 提交于 2019-12-03 11:00:35
问题 This question already has answers here : How to get the difference between two arrays in JavaScript? (63 answers) Closed 3 years ago . I have two array of object : the element of my table are not primitive value, but complexe objects. array1 = [obj1,obj2,obj3,obj4] array2 = [obj5,obj5,obj6,obj7] I would like to compare two arrays and see of the elements of array 2 are already present in array1 then create a new array of the difference. Any suggestion ? 回答1: var presents = _.intersectionWith

Sum all data in array of objects into new array of objects

我只是一个虾纸丫 提交于 2019-12-03 10:45:42
问题 I have an array of objects that looks like this: var data = [{costOfAirtickets: 2500, costOfHotel: 1200},{costOfAirtickets: 1500, costOfHotel: 1000}] and I want to sum each element in the array to produce an array like this: var result = [{costOfAirtickets: 4000, costOfHotel: 3200}] I have used a map and reduce function but I was able to only sum an individual element like so: data.map(item => ite.costOfAirtickets).reduce((prev, next)=>prev + next); // 22 At the moment this produces a single

Lodash title case (uppercase first letter of every word)

岁酱吖の 提交于 2019-12-03 10:24:42
问题 I'm looking through the lodash docs and other Stack Overflow questions - while there are several native JavaScript ways of accomplishing this task, is there a way I can convert a string to title case using purely lodash functions (or at least existing prototypal functions) so that I don't have to use a regular expression or define a new function? e.g. This string ShouLD be ALL in title CASe should become This String Should Be All In Title Case 回答1: This can be done with a small modification

Filter array of objects that contains string using lodash

▼魔方 西西 提交于 2019-12-03 09:59:43
I have a value and i need to return the objects that contains this value in propertie. var search='CPP@'; var results=_.filter(collection,{VAL:search}); I need to grab all objects that constains 'CPP@' , not the equals. I've prepared a https://jsfiddle.net/licass/e87mxfqt/ var collection=[ { "DSP_MAQ": "Máquina 4", "VAL": "CPP@4@1900-01-01" }, { "DSP_MAQ": "Máquina 5", "VAL": "CMIP@5@1900-01-01" }, { "DSP_MAQ": "Máquina 6", "VAL": "CMIP@6@1900-01-01" }, { "DSP_MAQ": "Máquina 7", "VAL": "CMIP@7@1900-01-01" }, { "DSP_MAQ": "Máquina 8", "VAL": "CPP@8@1900-01-01" }, { "DSP_MAQ": "Máquina 9", "VAL"

In Angular4 - How do I add lodash and do a build without errors

匿名 (未验证) 提交于 2019-12-03 08:57:35
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using angular.io quickstart seed: https://angular.io/docs/ts/latest/guide/setup.html I'm using Angular4 with typescript JIT Currently I would like to add lodash so I can use this in my component and then do an npm run build e.g. tsc -p src/" and not get any errors My component: import { Component } from '@angular/core'; import * as _ from 'lodash'; @Component({ moduleId: module.id, selector: 'HomeComponent', templateUrl: 'home.component.html' }) export class HomeComponent { constructor() { console.log(_.last([1, 2, 3])); console.log(

Sort array with lodash by value (integer)

試著忘記壹切 提交于 2019-12-03 08:06:51
问题 I'm really struggling on that but I cannot find a solution. I have an array and I want to sort it by value (all integers). I thought, well let's use lodash, there sure must be a handy function for that. Somehow I cannot figure out to do this though. So far I got this: myArray = [3, 4, 2, 9, 4, 2] I got a result if I used this code: myArray = _(myArray).sort(); But unfortunately the return value does not seem to be an array anymore. myArray.length is undefined after the sorting. I found

Find and replace value inside an array of objects javascript [duplicate]

人盡茶涼 提交于 2019-12-03 07:56:50
This question already has answers here : How to change value of object which is inside an array using JavaScript or jQuery? (19 answers) I have an array of objects: [{ { "enabled": true, "deviceID": "eI2K-6iUvVw:APA" }, { "enabled": true, "deviceID": "e_Fhn7sWzXE:APA" }, { "enabled": true, "deviceID": "e65K-6RRvVw:APA" }, }] A POST request is coming in with the deviceID of eI2K-6iUvVw:APA , all i want to do is to iterate the array, find the deviceID and change the enabled value to false . How's that possible in javascript? You can use Array#find . let arr = [{ "enabled": true, "deviceID":

Using Lodash to sum values by key

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I sum the values in objects which share a common key? I need to use Lodash for this because I need good performance if these arrays get huge. var prjMgrValues = [ {"proj_mgr":"Jack ProjManager","submitted_dollars":12000}, {"proj_mgr":"Jack ProjManager","submitted_dollars":750000}, {"proj_mgr":"Joe ProjManager","submitted_dollars":45000} ] I'm looking for an output of [ {"proj_mgr":"Jack ProjManager","submitted_dollars":762000}, {"proj_mgr":"Joe ProjManager","submitted_dollars":45000} ] 回答1: This is a case of reduction for each unique

How to deeply map object keys with JavaScript (lodash)?

*爱你&永不变心* 提交于 2019-12-03 07:07:55
问题 https://lodash.com/docs#mapKeys Is it possible to map an Object's keys deeply using Lodash? If not, is there another library providing this functionality (if grouped with other deep iteration and manipulation functionality, even better!)? Else, how would one go about implementing this? The main struggle I see is in identifying pure key/value objects that are safely, deeply iterable. It's easy to throw out Arrays, but it's important to note that the function shouldn't try to deeply iterate on