lodash

How do I use the includes method in lodash to check if an object is in the collection?

安稳与你 提交于 2019-11-30 04:08:27
lodash lets me check for membership of basic data types with includes : _.includes([1, 2, 3], 2) > true But the following doesn't work: _.includes([{"a": 1}, {"b": 2}], {"b": 2}) > false This confuses me because the following methods that search through a collection seem to do just fine: _.where([{"a": 1}, {"b": 2}], {"b": 2}) > {"b": 2} _.find([{"a": 1}, {"b": 2}], {"b": 2}) > {"b": 2} What am I doing wrong? How do I check for the membership of an object in a collection with includes ? edit: question was originally for for lodash version 2.4.1, updated for lodash 4.0.0 p.s.w.g The includes

What is the difference between lodash's _.map and _.pluck?

自古美人都是妖i 提交于 2019-11-30 02:58:49
问题 I have the following code, can anyone tell the difference: const _ = require('lodash'); const arr = [ {'fname':'Ali', 'lname': 'Yousuf'}, {'fname': 'Uzair', 'lname': 'Ali'}, {'fname': 'Umair', 'lname': 'Khan'} ]; _.map(arr, 'fname'); _.pluck(arr, 'fname'); The output is the same, and both functions are not mutating arr . 回答1: In the way you're using them, they basically do the same. That's why .pluck() was removed from Lodash v4.0.0 in favor of using .map() with a string as second argument.

React Native: Using lodash debounce

∥☆過路亽.° 提交于 2019-11-30 02:00:43
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. 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 as oppose to creating a new instance like it's happening now when you put it in the onChangeText handler

How do you chain functions using lodash?

纵饮孤独 提交于 2019-11-30 01:14:18
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? Kenneth To chain with lodash, you first have to wrap the object: _(foundUser).assignIn(rows[0]).omit([

lodash: mapping array to object

删除回忆录丶 提交于 2019-11-29 23:29:21
Is there a built-in lodash function to take this: var params = [ { name: 'foo', input: 'bar' }, { name: 'baz', input: 'zle' } ]; And output this: var output = { foo: 'bar', baz: 'zle' }; Right now I'm just using Array.prototype.reduce() : function toHash(array, keyName, valueName) { return array.reduce(function(dictionary, next) { dictionary[next[keyName]] = next[valueName]; return dictionary; }, {}); } toHash(params, 'name', 'input'); Wondering if there's a lodash short-cut. another way with lodash 4.17.2 _.chain(params) .keyBy('name') .mapValues('input') .value(); or _.mapValues(_.keyBy

lodash multi-column sortBy descending

本秂侑毒 提交于 2019-11-29 20:21:17
There's a nifty method to sort an array of objects based on several properties: var data = _.sortBy(array_of_objects, ['type', 'name']); However that is only for ascending sorting. Is there some handy way of defining direction per column? E.g. var data = _.sortBy(array_of_objects, [{'type': 'asc'}, {'name': 'desc'}]); As of lodash 3.5.0 you can use sortByOrder (renamed orderBy in v4.3.0): var data = _.sortByOrder(array_of_objects, ['type','name'], [true, false]); Since version 3.10.0 you can even use standard semantics for ordering (asc, desc): var data = _.sortByOrder(array_of_objects, ['type

transform object to array with lodash

▼魔方 西西 提交于 2019-11-29 20:12:56
How can I transform a big object to array with lodash? var obj = { 22: {name:"John", id:22, friends:[5,31,55], works:{books:[], films:[],} 12: {name:"Ivan", id:12, friends:[2,44,12], works:{books:[], films:[],} } // transform to var arr = [{name:"John", id:22...},{name:"Ivan", id:12...}] You can do var arr = _.values(obj); For documentation see here. _.toArray(obj); Outputs as: [ { "name": "Ivan", "id": 12, "friends": [ 2, 44, 12 ], "works": { "books": [], "films": [] } }, { "name": "John", "id": 22, "friends": [ 5, 31, 55 ], "works": { "books": [], "films": [] } } ]" A modern native solution

error TS2339: Property 'endsWith' does not exist on type 'string'

余生颓废 提交于 2019-11-29 18:43:30
问题 I get this error on the code block below. error TS2339: Property 'endsWith' does not exist on type 'string' let myList = angular.element(elem).attr("href").split("/"); let last = _.last<string>(myList); if (last.endsWith("something")) { return last; } I have also discovered this link that shows that there is a function endsWith(...) . http://definitelytyped.org/docs/typescript-services--typescriptServices/classes/typescript.stringutilities.html Do I miss some .d.ts file or what? 回答1: endsWith

How to compare two array of object and get common objects

断了今生、忘了曾经 提交于 2019-11-29 17:31:06
Hello guys I have two arrays var elements = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "type": "label", "id": "id_234" }, { "id": "id_16677", "type": "div", }, { "id": "id_155", "type": "input", "businesstype": { "type": "password" } } ] var filterArray=[{type:'input',businesstype:{type:'text'}},{type:'div'}] and want common obejct like var output = [{ "id": "id_1", "type": "input", "businesstype": { "type": "text" } }, { "id": "id_16677", "type": "div", } ] How do I compare these two objects to get equal objects from elements. You could filter it with a

Add new properties of object in lodash

我的梦境 提交于 2019-11-29 17:01:50
问题 I've two objects and I want to add properties from object A to object B and I try with extend which doesn't work,do I need to use something different ? a = { name = "value" name2 = "value2" } b = { name3 = "value" name4 = "value2" } I want that A will contain both a = { name = "value" name2 = "value2" name3 = "value" name4 = "value2" } 回答1: _.extend (now called _.assign) is indeed how you do this: _.assign(a, b); Live Example : var a = { name: "value", name2: "value2" }; var b = { name3: