lodash

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

余生颓废 提交于 2019-12-21 03:49:29
问题 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

Loop through properties in JavaScript object with Lodash

天大地大妈咪最大 提交于 2019-12-20 09:21:38
问题 Is it possible to loop through the properties in a JavaScript object? For instance, I have a JavaScript object defined as this: myObject.options = { property1: 'value 1', property2: 'value 2' }; Properties will get dynamically added to this object. Is there a way for me to just loop through and do a check if a property exists? If so, how? 回答1: Yes you can and lodash is not needed... i.e. for (var key in myObject.options) { // check also if property is not inherited from prototype if (myObject

What's the difference between transform and reduce in lodash

痞子三分冷 提交于 2019-12-20 08:46:54
问题 Other than stating "transform is a more powerful alternative to reduce", I can find no documentation of what the differences are. What are the differences between transform and reduce in lodash (Other than it being 25% slower)? 回答1: I like to dive into the source code before I pull in utilities. For lo-dash this can be difficult as there is a ton of abstracted internal functionality in all the utilities. transform source reduce source So the obvious differences are: If you dont specify the

LoDash: Get an array of values from an array of object properties

感情迁移 提交于 2019-12-20 08:04:04
问题 I'm sure it's somewhere inside the LoDash docs, but I can't seem to find the right combination. var users = [{ id: 12, name: Adam },{ id: 14, name: Bob },{ id: 16, name: Charlie },{ id: 18, name: David } ] // how do I get [12, 14, 16, 18] var userIds = _.map(users, _.pick('id')); 回答1: Since version v4.x you should use _.map: _.map(users, 'id'); // [12, 14, 16, 18] this way it is corresponds to native Array.prototype.map method where you would write (ES2015 syntax): users.map(user => user.id);

Javascript ES6/ES5 find in array and change

陌路散爱 提交于 2019-12-20 07:59:28
问题 I have an array of objects. I want to find by some field, and then to change it: var item = {...} var items = [{id:2}, {id:2}, {id:2}]; var foundItem = items.find(x => x.id == item.id); foundItem = item; I want it to change the original object. How? (I dont care if it will be in lodash too) 回答1: You can use findIndex to find the index in the array of the object and replace it as required: var item = {...} var items = [{id:2}, {id:2}, {id:2}]; var foundIndex = items.findIndex(x => x.id == item

How to search both parent and child object in Javascript/Lodash/ES6?

痴心易碎 提交于 2019-12-20 07:47:46
问题 So I'm using Vue to filter a list. This consists of the following structure (Baum Hierarchy if anyone is interested): [ { "id": 61, "created_at": "2016-11-23 22:07:10", "updated_at": "2016-12-15 19:44:56", "parent_id": null, "lft": 107, "rgt": 116, "depth": 0, "name": "Quia eos voluptas molestiae ut cum.", "slug": "consequatur-voluptatum-dolores-non-perferendis-possimus", "order_column": null, "belongs_to": null, "children": [ { "id": 57, "created_at": "2016-11-23 22:07:10", "updated_at":

Flatten a nested array of objects over a array field

女生的网名这么多〃 提交于 2019-12-20 07:13:02
问题 Have an object a1 = [{name:'x',age:21, addr:[{flat:1,add:'xyz'},{flat:2,add:'xsr'}]}, {name:'y',age:22, addr:[{flat:3,add:'xyz1'},{flat:4,add:'xsr1'}]] Desired output: [{name:'x',age:21, addr:{flat:1,add:'xyz'}}, {name:'x',age:21, addr:{flat:2,add:'xsr'}}, {name:'y',age:22, addr:{flat:3,add:'xyz1'}, {name:'y',age:22, addr:{flat:4,add:'xsr1'}] Please suggest! I am trying to accomplish this using lodash/underscore. 回答1: Map every item in the original array to a new array, with a number of items

merge array object together - lodash

耗尽温柔 提交于 2019-12-20 02:08:33
问题 So I have an array of items like this: items = [ { amount: 2, name: 'bike' }, { amount: 1, name: 'boat' }, { amount: 3, name: 'bike' } ] Now, I would like to merge this array so that there would be no duplicates of bike and still know how many bikes there are in total. so my result array would look like this: items = [ { amount: 5, name: 'bike' }, { amount: 1, name: 'boat' } ] In order to keep my code short I have been advised using lodash and I've been looking at the different ways to merge

Join sequence of arrays with array delimeters (“intersperse”)

时光总嘲笑我的痴心妄想 提交于 2019-12-20 01:35:13
问题 Is there a function that lets me concat several arrays, with delimiters between them (the delimiters are also arrays), similarly to how join works but not restricted to strings? The function can be standard JS or part of a major library such as lodash (which is why it's referenced in the tags). Here is an example of usage: let numbers = [[1], [2], [3]]; let result = _.joinArrays(numbers, [0]); console.log(result); //printed: [1, 0, 2, 0, 3] This is analogous to: let strings = ["a", "b", "c"];

Join sequence of arrays with array delimeters (“intersperse”)

為{幸葍}努か 提交于 2019-12-20 01:35:07
问题 Is there a function that lets me concat several arrays, with delimiters between them (the delimiters are also arrays), similarly to how join works but not restricted to strings? The function can be standard JS or part of a major library such as lodash (which is why it's referenced in the tags). Here is an example of usage: let numbers = [[1], [2], [3]]; let result = _.joinArrays(numbers, [0]); console.log(result); //printed: [1, 0, 2, 0, 3] This is analogous to: let strings = ["a", "b", "c"];