lodash

Terse way to intersperse element between all elements in JavaScript array?

血红的双手。 提交于 2019-12-18 04:33:40
问题 Say I have an array var arr = [1, 2, 3] , and I want to separate each element by an element eg. var sep = "&" , so the output is [1, "&", 2, "&", 3] . Another way to think about it is I want to do Array.prototype.join ( arr.join(sep) ) without the result being a string (because the elements and separator I am trying to use are Objects, not strings). Is there a functional/nice/elegant way to do this in either es6/7 or lodash without something that feels clunky like: _.flatten(arr.map((el, i) =

Filtering object properties based on value

懵懂的女人 提交于 2019-12-18 04:01:08
问题 Is there some elegant way of filtering out falsey properties from this object with lodash/underscore? Similar to how _.compact(array) removes falsey elements from arrays so from { propA: true, propB: true, propC: false, propD: true, } returning { propA: true, propB: true, propD: true, } 回答1: Prior to Lodash 4.0 You want _.pick , it takes a function as an argument and returns an object only containing the keys for which that function returns truthy. So you can do: filtered = _.pick(obj,

Joins in Javascript

我是研究僧i 提交于 2019-12-17 23:37:18
问题 I have 2 lists of objects: people = [{id: 1, name: "Tom", carid: 1}, {id: 2, name: "Bob", carid: 1}, {id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2}]; cars= [{id: 1, name: "Ford Fiesta", color: "blue"}, {id: 2, name: "Ferrari", color: "red"}, {id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}]; Is there a function (possibly in Angular, JQuery, Underscore, LoDash, or other external library) to do a left join in one line on these? Something like: peoplewithcars

using promises in node.js to create and compare two arrays

故事扮演 提交于 2019-12-17 21:21:50
问题 I needed to compare two arrays the first one a couple of filenames from a database, the second one a list of files I already downloaded to my client. The Idea was to load whatever files are missing on the client. As the reading via fs was two slow, I tried using Promises to wait for one function to finish before the next starts. But somehow I got lost... My code so far: let filesIneed = []; let filesIhave = []; let filesToFetch = []; getLocalFiles().then(getFilesIneed).then(getfilesToRetreive

Group By and Sum using Underscore/Lodash

十年热恋 提交于 2019-12-17 18:44:18
问题 I have JSON like this: [ { platformId: 1, payout: 15, numOfPeople: 4 }, { platformId: 1, payout: 12, numOfPeople: 3 }, { platformId: 2, payout: 6, numOfPeople: 5 }, { platformId: 2, payout: 10, numOfPeople: 1 }, ] And I want to Group it by platformId with sum of payout and numOfPeople . I.e. in result I want JSON like this: [ "1": { payout: 27, numOfPeople: 7 }, "2": { payout: 16, numOfPeople: 6 } ] I tried to use underscore.js 's _.groupBy method, and it groups fine, but how I can get the

Find object by match property in nested array

穿精又带淫゛_ 提交于 2019-12-17 18:12:11
问题 I'm not seeing a way to find objects when my condition would involve a nested array. var modules = [{ name: 'Module1', submodules: [{ name: 'Submodule1', id: 1 }, { name: 'Submodule2', id: 2 } ] }, { name: 'Module2', submodules: [{ name: 'Submodule1', id: 3 }, { name: 'Submodule2', id: 4 } ] } ]; This won't work because submodules is an array, not an object. Is there any shorthand that would make this work? I'm trying to avoid iterating the array manually. _.where(modules, {submodules:{id:3}}

lodash debounce not working in anonymous function

扶醉桌前 提交于 2019-12-17 18:11:30
问题 Hello I cannot seem to figure out why the debounce function works as expected when passed directly to a keyup event; but it does not work if I wrap it inside an anonymous function. I have fiddle of the problem: http://jsfiddle.net/6hg95/1/ EDIT: Added all the things I tried. HTML <input id='anonFunction'/> <input id='noReturnAnonFunction'/> <input id='exeDebouncedFunc'/> <input id='function'/> <div id='output'></div> JAVASCRIPT $(document).ready(function(){ $('#anonFunction').on('keyup',

Find object by match property in nested array

随声附和 提交于 2019-12-17 18:08:36
问题 I'm not seeing a way to find objects when my condition would involve a nested array. var modules = [{ name: 'Module1', submodules: [{ name: 'Submodule1', id: 1 }, { name: 'Submodule2', id: 2 } ] }, { name: 'Module2', submodules: [{ name: 'Submodule1', id: 3 }, { name: 'Submodule2', id: 4 } ] } ]; This won't work because submodules is an array, not an object. Is there any shorthand that would make this work? I'm trying to avoid iterating the array manually. _.where(modules, {submodules:{id:3}}

Javascript: Sort array of arrays by second element in each inner array

帅比萌擦擦* 提交于 2019-12-17 16:59:28
问题 I have an array that looks like this: const arr = [ [500, 'Foo'], [600, 'bar'], [700, 'Baz'], ]; I would like to sort this arr alphabetically by the second element in each inner array, ie: [ [600, 'bar'], [700, 'Baz'], [500, 'Foo'], ] Note the case insensitivity. Also, I would love to use lodash helpers if they come in handy here! 回答1: Here is a concrete, working example, using Array.prototype.sort: const arr = [ [500, 'Foo'], [600, 'bar'], [700, 'Baz'] ]; arr.sort((a,b) => a[1].toUpperCase()

Can't perform a React state update on an unmounted component

眉间皱痕 提交于 2019-12-17 16:31:57
问题 Problem I am writing an application in React and was unable to avoid a super common pitfall, which is calling setState(...) after componentWillUnmount(...) . I looked very carefully at my code and tried to put some guarding clauses in place, but the problem persisted and I am still observing the warning. Therefore, I've got two questions: How do I figure out from the stack trace , which particular component and event handler or lifecycle hook is responsible for the rule violation? Well, how