underscore.js

Sorting Javascript array of objects by specific string order?

风格不统一 提交于 2020-06-27 05:56:13
问题 I have the following data: and I was wondering how I could sort these in a specific order. The order needs to be: Yellow, Blue, White, Green, Red and then within those colors, it would show the smallest number first. So in this case, the correct sorting should be: y2, y3, b0, b2, b6, w2, g7, r4 Does anyone have any ideas on how to accomplish that? I'm using underscore.js if that makes it easier. 回答1: This naively assumes that all elements have a valid color (or at least it sorts elements with

Return an object key only if value is true

為{幸葍}努か 提交于 2020-06-25 07:41:08
问题 How do i return an object key name only if the value of it is true? I'm using underscore and the only thing i see is how to return keys which is easy, i want to avoid redundant iterations as much as possible: example: Object {1001: true, 1002: false} I want an array with only 1001 in it... 回答1: Object.keys gets the keys from the object, then you can filter the keys based on the values var obj = {1001: true, 1002: false}; var keys = Object.keys(obj); var filtered = keys.filter(function(key) {

Javascript how to sort array of objects by numeric value of the string [duplicate]

半腔热情 提交于 2020-06-23 09:46:12
问题 This question already has answers here : Sort array of strings by integer (which is the first character) (3 answers) Sort Array Elements (string with numbers), natural sort (8 answers) Closed 2 years ago . Below is an example of my array of objects and I am supposed to sort these by name. The js normal sorting sorts as shown below, however my target is to sort by the numeric value, eg: "1 ex, 2 ex, 3 ex, 4 ex, 11 ex ..". Could someone help me how to achieve this? [{id: 588, name: "1 ex"} {id:

Javascript how to sort array of objects by numeric value of the string [duplicate]

China☆狼群 提交于 2020-06-23 09:45:38
问题 This question already has answers here : Sort array of strings by integer (which is the first character) (3 answers) Sort Array Elements (string with numbers), natural sort (8 answers) Closed 2 years ago . Below is an example of my array of objects and I am supposed to sort these by name. The js normal sorting sorts as shown below, however my target is to sort by the numeric value, eg: "1 ex, 2 ex, 3 ex, 4 ex, 11 ex ..". Could someone help me how to achieve this? [{id: 588, name: "1 ex"} {id:

How to get first n elements of an object using lodash?

夙愿已清 提交于 2020-05-12 14:02:03
问题 I want to get the first n key/value pairs from an object (not an array) using lodash. I found this answer for underscore, which says to use use first (doesn't exist in lodash), or to use take (only works on arrays). Sample node session trying to get the 'a:7' and 'b:8' pairs from an object: > var ld=require("lodash") undefined > var o={a:7, b:8, c:9} undefined > ld.keys(o) [ 'a', 'b', 'c' ] > ld.take(o, 2) [] > ld.first(o, 2) undefined > Surely, there must be some easy way to do this with

How to get first n elements of an object using lodash?

£可爱£侵袭症+ 提交于 2020-05-12 13:59:06
问题 I want to get the first n key/value pairs from an object (not an array) using lodash. I found this answer for underscore, which says to use use first (doesn't exist in lodash), or to use take (only works on arrays). Sample node session trying to get the 'a:7' and 'b:8' pairs from an object: > var ld=require("lodash") undefined > var o={a:7, b:8, c:9} undefined > ld.keys(o) [ 'a', 'b', 'c' ] > ld.take(o, 2) [] > ld.first(o, 2) undefined > Surely, there must be some easy way to do this with

Underscore debounce vs vanilla Javascript setTimeout

早过忘川 提交于 2020-05-10 04:44:07
问题 I understand that debounce in Undercore.js returns a function that will postpone its execution until the wait time is over. My question is, is there an advantage of using debounce over the regular setTimeout function in vanilla Javascript? Don't they both work the same? 回答1: They are very different and used in completely different cases. _.debounce returns a function , setTimeout returns an id which you can use to cancel the timeOut. No matter how many times you call the function which is

Underscore debounce vs vanilla Javascript setTimeout

痴心易碎 提交于 2020-05-10 04:42:12
问题 I understand that debounce in Undercore.js returns a function that will postpone its execution until the wait time is over. My question is, is there an advantage of using debounce over the regular setTimeout function in vanilla Javascript? Don't they both work the same? 回答1: They are very different and used in completely different cases. _.debounce returns a function , setTimeout returns an id which you can use to cancel the timeOut. No matter how many times you call the function which is

Grouping a sequence of numbers with no gaps in javascript using underscore.js

99封情书 提交于 2020-02-29 03:52:09
问题 Having an array of numbers [1, 2, 3, 4, 7, 8, 11, 15, 16, 17, 18] , how can we group them in groups of consecutive numbers using underscore.js. So the desired output is 4 groups (1-4, 7-8, 11 and 15-18) [[1, 2, 3, 4], [7, 8], [11], [15, 16, 17, 18]] 回答1: I would just do it with reduce and not worry about another library. [1, 2, 3, 4, 7, 8, 11, 15, 16, 17, 18].reduce((arr, val, i, a) => { if (!i || val !== a[i - 1] + 1) arr.push([]); arr[arr.length - 1].push(val); return arr; }, []); var

how to find difference between two array using lodash/underscore in nodejs

大兔子大兔子 提交于 2020-02-27 14:10:10
问题 I have two arrays of arrays and am trying to find the difference. var a = [[ 11, 24, 28, 38, 42, 44 ], [ 7, 19, 21, 22, 29, 38 ], [ 2, 21, 27, 30, 33, 40 ], [ 6, 11, 12, 21, 34, 48 ], [ 1, 10, 17, 31, 35, 40 ], [ 1, 18, 26, 33, 36, 45 ], [ 15, 21, 22, 24, 38, 46 ], [ 5, 17, 21, 27, 29, 41 ], [ 3, 7, 12, 16, 20, 28 ], [ 9, 12, 13, 18, 30, 37 ], [ 3, 19, 21, 31, 33, 46 ], [ 6, 11, 16, 18, 20, 34 ], [ 1, 3, 11, 13, 24, 28 ], [ 12, 13, 16, 40, 42, 46 ], [ 1, 3, 5, 36, 37, 41 ], [ 14, 15, 23, 24,