lodash

Using lodash to check whether an array has duplicate values

我们两清 提交于 2019-11-29 16:33:01
问题 What do you all think would be the best (best can be interpreted as most readable or most performant, your choice) way to write a function using the lodash utilities in order to check an array for duplicate values. I want to input ['foo', 'foo', 'bar'] and have the function return true . And input ['foo', 'bar', 'baz'] and have the function return false . 回答1: You can try this code: function hasDuplicates(a) { return _.uniq(a).length !== a.length; } var a = [1,2,1,3,4,5]; var b = [1,2,3,4,5,6

Finding nested duplicate arrays in JavaScript. (Nested Array uniq in lodash/underscore)

陌路散爱 提交于 2019-11-29 15:37:16
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 the array contains duplicate arrays? It's a little sloppy, but (possible) var arr2 = _.uniq(arr1,

Intersection of characters in two strings

浪子不回头ぞ 提交于 2019-11-29 12:51:06
I have an object with strings in it. filteredStrings = {search:'1234', select:'1245'} I want to return '124' I know that I can turn it into an array and then loop through each value and test if that value in inside of the other string, but I'm looking for an easier way to do this. Preferably with Lodash. I've found _.intersection(Array,Array) but this only works with Arrays. https://lodash.com/docs#intersection I want to be able to do this without having to convert the object to an array and then loop through each value because this is going to be potentially holding a lot of information and I

Find all objects with matching Ids javascript

送分小仙女□ 提交于 2019-11-29 12:27:39
I'm trying to get all objects with matching id's from my students array and get other property values from them... For instance my array looks like this: const students = [ {id: 1, name: 'Cal', location: 'McHale' }, {id: 2, name: 'Courtney', location: 'Sydney Hall' }, {id: 1, name: 'Cal', location: 'Syndey hall' } ] So my expected output would grab all instances of id: 1. {id: 1, name: 'Cal', location: 'McHale' }, {id: 1, name: 'Cal', location: 'Syndey hall' } I'll eventually want to remove duplicate names and display in a list like so... (But that's down the line. For now I just want to grab

Iterate an array as a pair (current, next) in JavaScript

北城以北 提交于 2019-11-29 03:54:39
In the question Iterate a list as pair (current, next) in Python , the OP is interested in iterating a Python list as a series of current, next pairs. I have the same problem, but I'd like to do it in JavaScript in the cleanest way possible, perhaps using lodash . It is easy to do this with a simple for loop, but it doesn't feel very elegant. for (var i = 0; i < arr.length - 1; i++) { var currentElement = arr[i]; var nextElement = arr[i + 1]; } Lodash almost can do this: _.forEach(_.zip(arr, _.rest(arr)), function(tuple) { var currentElement = tuple[0]; var nextElement = tuple[1]; }) The

lodash: Get duplicate values from an array

无人久伴 提交于 2019-11-29 02:59:01
Say I have an array like this: [1, 1, 2, 2, 3] I want to get the duplicates which are in this case: [1, 2] Does lodash support this? I want to do it in the shortest way possible. You can use this: _.filter(arr, (val, i, iteratee) => _.includes(iteratee, val, i + 1)); Note that if a number appears more than two times in your array you can always use _.uniq . Another way is to group by unique items, and return the group keys that have more than 1 item _([1, 1, 2, 2, 3]).groupBy().pickBy(x => x.length > 1).keys().value() var array = [1, 1, 2, 2, 3]; var groupped = _.groupBy(array, function (n)

lodash: Get duplicate values from an array

醉酒当歌 提交于 2019-11-29 02:57:15
Say I have an array like this: [1, 1, 2, 2, 3] I want to get the duplicates which are in this case: [1, 2] Does lodash support this? I want to do it in the shortest way possible. You can use this: _.filter(arr, (val, i, iteratee) => _.includes(iteratee, val, i + 1)); Note that if a number appears more than two times in your array you can always use _.uniq . Another way is to group by unique items, and return the group keys that have more than 1 item _([1, 1, 2, 2, 3]).groupBy().pickBy(x => x.length > 1).keys().value() var array = [1, 1, 2, 2, 3]; var groupped = _.groupBy(array, function (n)

How to use throttle or debounce with React Hook?

橙三吉。 提交于 2019-11-29 01:28:16
I'm trying to use the throttle method from lodash in a fonctionnal component, e.g.: const App = () => { const [value, setValue] = useState(0) useEffect(throttle(() => console.log(value), 1000), [value]) return ( <button onClick={() => setValue(value + 1)}>{value}</button> ) } Since the method inside useEffect is redeclared at each render, the throttling effect does not work. Does anyone have a simple solution ? you may(and probably need) useRef to store value between renders. Just like it's suggested for timers Something like that const App = () => { const [value, setValue] = useState(0) const

lodash orderBy on nested property

安稳与你 提交于 2019-11-28 23:04:04
I'm using v4.11.0 . I would like sort objects based on milliseconds property. Here's the array : [ { "name": "bug12755.xml", "list": "bugs42", "start-date": "2015-09-14", "age": { "text": "7 months", "milliseconds": 18381227304 } }, { "name": "bug12922.xml", "list": "bugs42", "start-date": "2015-08-27", "age": { "text": "8 months", "milliseconds": 19936427304 } }, { "name": "bug13183.xml", "list": "bugs50", "start-date": "2015-08-27", "age": { "text": "8 months", "milliseconds": 19936427305 } } ] I'm missing something fundamental about the iteratee function. I have this but does not seem to

React Native: Using lodash debounce

白昼怎懂夜的黑 提交于 2019-11-28 22:49:20
问题 I'm playing around with React Native and lodash's debounce. Using the following code only make it work like a delay and not a debounce. <Input onChangeText={(text) => { _.debounce(()=> console.log("debouncing"), 2000)() } /> I want the console to log debounce only once if I enter an input like "foo". Right now it logs "debounce" 3 times. 回答1: Debounce function should be defined somewhere outside of render method since it has to refer to the same instance of the function every time you call it