lodash

underscore/lodash unique by multiple properties

孤街醉人 提交于 2019-11-28 21:11:52
I have an array of objects with duplicates and I'm trying to get a unique listing, where uniqueness is defined by a subset of the properties of the object. For example, {a:"1",b:"1",c:"2"} And I want to ignore c in the uniqueness comparison. I can do something like _.uniq(myArray,function(element) { return element.a + "_" + element+b}); I was hoping I could do _.uniq(myArray,function(element) { return {a:element.a, b:element.b} }); But that doesn't work. Is there something like that I can do, or do I need to create a comparable representation of the object if I'm comparing multiple properties?

Lodash .clone and .cloneDeep behaviors

主宰稳场 提交于 2019-11-28 21:06:54
I try to clone an array of objects with nested objects. Something like: var data = [ { id: 1, values: { a: 'a', b: 'b' } }, { id: 2, values: { c: 'c', d: 'd' } } ]; _.Clone With the _.clone method and the isDeep parameter at true : var clone = _.clone(data, true); data[1].values.d = 'x'; console.log( _.isEqual(data, clone) ); // true, clone[1].values.d == 'x' I expected clone[1].values.d == 'd' : If isDeep is true nested objects will also be cloned, otherwise they will be assigned by reference. What is wrong? _.CloneDeep In addition, when I try with the _.cloneDeep method, I get an error: var

lodash debounce not working in anonymous function

a 夏天 提交于 2019-11-28 21:02:06
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', function () { return _.debounce(debounceIt, 500, false); //Why does this differ from #function }); $('

Lodash : how to do a case insensitive sorting on a collection using orderBy?

前提是你 提交于 2019-11-28 21:00:22
I checked this answer but to achieve the same result, that is to get case-insensitive sorting, I need to use orderBy instead of sortBy since it gives the ability to specify the sort order . The only way I found to achieve it was to create a cloned "middle" array mapped to lower case the name : const users = [ { name: 'A', age: 48 }, { name: 'B', age: 34 }, { name: 'b', age: 40 }, { name: 'a', age: 36 } ]; let lowerCaseUsers = _.clone(users); lowerCaseUsers = lowerCaseUsers.map((user) => { user.name = user.name.toLowerCase(); return user; }); const sortedUsers = _.orderBy(lowerCaseUsers, ['name

vue watcher

泪湿孤枕 提交于 2019-11-28 20:34:29
观察 Watchers 虽然计算属性在大多数情况下更合适,但有时也需要一个自定义的 watcher 。这是为什么 Vue 提供一个更通用的方法通过 watch 选项,来响应数据的变化。当你想要在数据变化响应时,执行异步操作或开销较大的操作,这是很有用的。 例如: 在这个示例中,使用 watch 选项允许我们执行异步操作(访问一个 API),限制我们执行该操作的频率,并在我们得到最终结果前,设置中间状态。这是计算属性无法做到的。 <div id="watch-example"> <p> Ask a yes/no question: <input v-model="question"> </p> <p>{{ answer }}</p> </div> <!-- Since there is already a rich ecosystem of ajax libraries --> <!-- and collections of general-purpose utility methods, Vue core --> <!-- is able to remain small by not reinventing them. This also --> <!-- gives you the freedom to just use what you're familiar with. --

Joins in Javascript

ぐ巨炮叔叔 提交于 2019-11-28 19:42: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 = leftjoin( people, cars, "carid", "id"); I can write my own, but if LoDash has an optimised version I

How to add mixins to ES6 javascript classes?

人盡茶涼 提交于 2019-11-28 18:49:16
In an ES6 class with some instance variables and methods, how can you add a mixin to it? I've given an example below, though I don't know if the syntax for the mixin object is correct. class Test { constructor() { this.var1 = 'var1' } method1() { console.log(this.var1) } test() { this.method2() } } var mixin = { var2: 'var2', method2: { console.log(this.var2) } } If I run (new Test()).test() , it will fail because there's no method2 on the class, as it's in the mixin, that's why I need to add the mixin variables and methods to the class. I see there's a lodash mixin function https://lodash.com

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

风流意气都作罢 提交于 2019-11-28 18:04:13
问题 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:

is there a function in lodash to replace matched item

烂漫一生 提交于 2019-11-28 16:26:44
I wonder if there is a simpler method in lodash to replace an item in a JavaScript collection? (Possible duplicate but I did not understand the answer there:) I looked at their documentation but could not find anything My code is: var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}]; // Can following code be reduced to something like _.XX(arr, {id:1}, {id:1, name: "New Name"}); _.each(arr, function(a, idx){ if(a.id === 1){ arr[idx] = {id:1, name: "Person New Name"}; return false; } }); _.each(arr, function(a){ document.write(a.name); }); Update: The object I'm trying to replace with

lodash multi-column sortBy descending

我是研究僧i 提交于 2019-11-28 16:04:46
问题 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'}]); 回答1: 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