lodash

underscore where with or condition (underscore, lodash or any other solution)

让人想犯罪 __ 提交于 2019-12-21 19:25:19
问题 I implemented a mixin to add "or" condition with _.where var arr = [{a:1,b:4}, {a:5}, {a:6}, {a:11}]; _.mixin({ or: function(obj,arr,condition){ return _.chain(arr).where(condition).union(obj).value(); } }); now i can use it like this and it works perfectly somewhat like a sql query _.chain(arr).where({a:1}).or(arr,{a:11,b:3}).or(arr,{a:2}).value(); //returns [{a:1,b:4}] _.chain(arr).where({a:1}).or(arr,{a:11}).or(arr,{a:2}).value(); //returns [{a:1,b:4},{a:11}] _.chain(arr).where({a:1}).or

How do I find the intersection of an array of arrays that contain objects using Javascript/underscorejs?

孤者浪人 提交于 2019-12-21 12:29:15
问题 I can't figure out how to find the intersection of this set of arrays: [ [ {"name":"product1","light":"1"}, {"name":"product2","light":"2"}, {"name":"product5","light":"5"}, {"name":"product4","light":"4"} ], [ {"name":"product2","light":"2"}, {"name":"product3","light":"3"}, {"name":"product4","light":"4"} ],[...more arrays with objects] ] This is just sample data , the real set I have changes a lot but with that structure. I want the returned intersection to look like this (a single array

How to delete recursively undefined properties from an object - while keeping the constructor chain?

不羁岁月 提交于 2019-12-21 08:15:47
问题 This is a question similar to How to remove undefined and null values from an object using lodash?. However, the solutions proposed there do not conserve the constructor. In addition to that, I want to only delete those keys which starts, say with '_'. Here is what I am looking for, and can't seem to manage to get from lodash : Input : new Cons({key1 : 'value1', key2 : {key21 : 'value21', _key22: undefined}, key3: undefined, _key4 : undefined}) Output : {key1 : 'value1', key2 : {key21 :

How to delete recursively undefined properties from an object - while keeping the constructor chain?

一个人想着一个人 提交于 2019-12-21 08:15:35
问题 This is a question similar to How to remove undefined and null values from an object using lodash?. However, the solutions proposed there do not conserve the constructor. In addition to that, I want to only delete those keys which starts, say with '_'. Here is what I am looking for, and can't seem to manage to get from lodash : Input : new Cons({key1 : 'value1', key2 : {key21 : 'value21', _key22: undefined}, key3: undefined, _key4 : undefined}) Output : {key1 : 'value1', key2 : {key21 :

lodash _.contains one of multiple values in string

柔情痞子 提交于 2019-12-21 07:42:39
问题 Is there a way in lodash to check if a strings contains one of the values from an array? For example: var text = 'this is some sample text'; var values = ['sample', 'anything']; _.contains(text, values); // should be true var values = ['nope', 'no']; _.contains(text, values); // should be false 回答1: Another solution, probably more efficient than looking for every values, can be to create a regular expression from the values. While iterating through each possible values will imply multiple

Set all Object keys to false

被刻印的时光 ゝ 提交于 2019-12-21 07:20:38
问题 Lets say I have an object filter: { "ID": false, "Name": true, "Role": false, "Sector": true, "Code": false } I want to set all keys to false (to reset them). What's the best way to do this, I'd like to avoid looping with foreach and stuff. Any neat one liner? 回答1: Using lodash, mapValues is a graceful, loop-free way: filter = { "ID": false, "Name": true, "Role": false, "Sector": true, "Code": false }; filter = _.mapValues(filter, () => false); If you want to do this with Underscore.js, there

Object.assign vs lodash _.assign

ぃ、小莉子 提交于 2019-12-21 07:12:19
问题 Looking at the documentation for ES6 Object.assign and Lodash _.assign it looks like these function in exactly the same way. Is that a correct understanding? Or am I missing something? 回答1: Depends on the browser. Per the lodash docs: Made _.assign use built-in Object.assign when available. You can go here for browser support: Basically, IE doesn't have support so lodash's code is used in that case MDN Docs on Object.assign 来源: https://stackoverflow.com/questions/37123425/object-assign-vs

Lo-Dash, difference between array and collection

北战南征 提交于 2019-12-21 07:02:44
问题 A glance at the Lo-Dash docs shows that the API falls in to categories of: Arrays, Chaining, Collections, Functions, Objects, Utilities, Methods, and Properties A more detailed look in to the Arrays API shows approximately 30 different methods available that are applicable to arrays. The Collections API has a few more methods than the Arrays API, and they do not share the same methods. Within the Collections API, a collection is described as an object that is iterated, and may be an array:

Filter array of objects with another array of object

拈花ヽ惹草 提交于 2019-12-21 05:43:10
问题 I want to Filter an array of objects with another array of object.The logic will be used in product search by category and color etc. This is my main Array : products: [{ id: 1, name: 'Product 1', category: 'Home', skill: 'Easy', color: 'blue', price: 100.00 }, { id: 2, name: 'Product 2', category: 'Home', skill: 'Intermediate', color: 'red', price: 120.00 }, { id: 3, name: 'Product 3', category: 'Office', skill: 'Intermediate', color: 'green', price: 190.00 }, { id: 4, name: 'Product 4',

javascript - lodash - create a unique list based on multiple attributes

最后都变了- 提交于 2019-12-21 03:49:41
问题 My collection looks like this. var list = [{id:'12345', sequence:null}, {id:'12346', sequence:null}, {id:'12347', sequence:null}, {id:'12348', sequence:1}, {id:'12348', sequence:2}, {id:'12349', sequence:1}, {id:'12349', sequence:1}]; I am trying to get a unique list so that object with same id AND sequence will only return one of the objects (we have 2 here- {id:'12349', sequence:1}) my code var uniqueList = _.uniq(list, function(obj) { return obj.id && obj.sequence; }); can we use lodash