lodash

How to get an array of values based on an array of indexes?

倖福魔咒の 提交于 2019-12-28 15:31:14
问题 I have a set of two arrays. One contains some fruit values as strings, the other one contains some random numbers. Here I considered the number arrays are the indexes of the fruits array. How to get a new array of fruits given the numbers in the index array? Sample code: var resultArr = []; var fruitier = ["apple", "orange", "grapes", "pineapple", "fig", "banana", "jackfruit", "pomegranate"]; var indexArr = [0, 2, 4]; Output: resultArr = ["apple", "grapes", "fig"]; 回答1: for(var i = 0; i <

lodash: Get duplicate values from an array

点点圈 提交于 2019-12-28 13:46:25
问题 Say I have an array like this: [1, 1, 2, 2, 3] I want to get the duplicates which are in this case: [1, 2] Does lodash support this? I want to do it in the shortest way possible. 回答1: You can use this: _.filter(arr, (val, i, iteratee) => _.includes(iteratee, val, i + 1)); Note that if a number appears more than two times in your array you can always use _.uniq . 回答2: Another way is to group by unique items, and return the group keys that have more than 1 item _([1, 1, 2, 2, 3]).groupBy()

Why is lodash.each faster than native forEach?

久未见 提交于 2019-12-28 04:53:10
问题 I was trying to find the fastest way of running a for loop with its own scope. The three methods I compared were: var a = "t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t".split(); // lodash .each -> 1,294,971 ops/sec lodash.each(a, function(item) { cb(item); }); // native .forEach -> 398,167 ops/sec a.forEach(function(item) { cb(item); }); // native for -> 1,140,382 ops/sec var lambda = function(item) { cb(item); }; for (var ix = 0, len = a

Lodash create collection from duplicate object keys

情到浓时终转凉″ 提交于 2019-12-28 04:27:09
问题 I have the following structure: var output = [{ "article": "BlahBlah", "title": "Another blah" }, { "article": "BlahBlah", "title": "Return of the blah" }, { "article": "BlahBlah2", "title": "The blah strikes back" }, { "article": "BlahBlah2", "title": "The blahfather" }] From the above using an elegant lodash one-liner, I need to create the following structure. var newOutput = [{ "article": "BlahBlah", "titles": ["Another blah", "Return of the blah"] }, { "article": "BlahBlah2", "titles": [

Lodash remove duplicates from array

半世苍凉 提交于 2019-12-28 01:43:14
问题 This is my data: [ { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello-how-are-you', id: "23" }, { url: 'www.example.com/i-like-cats', id: "24" }, { url: 'www.example.com/i-like-pie', id: "25" } ] With Lodash, how could I remove objects with duplicate id keys? Something with filter, map and unique, but not quite sure. My real data set is much larger and has more keys, but the concept should be the same. 回答1: _.unique no longer

Add propertie to object if property exists in another object using lodash

浪子不回头ぞ 提交于 2019-12-25 19:03:40
问题 I have two objects and I need to add properties to one object based on match from properties of another: var dependsOn= { qTableIdent: { //External object key mapping( object key : external key) 'RHID': 'RHID', 'CD_AGREGADO': 'NOME' } }, var tableCols= [ { "targets": 1, "title": 'RHID', "label": 'RHID', "data": 'RHID', "name": 'RHID', "width": "5%", "filter": true, "defaultContent": "", "visible":false, "type": 'hidden', //ADD this to properties attr: { 'name': 'rhid' }, },..... ] I would

lodash Mapping Keys from one object with values in array of objects

偶尔善良 提交于 2019-12-25 17:45:51
问题 I have the following object: var kmap={ key1:"useless", key2:"useless", key3:"useless" }; I need to map its keys with values of this array of Objects: var incoming=[ {value:"asd"}, {value:"qwe"}, {value:"zxc"} ]; Result: { key1:"asd", key2:"qwe", key3:"zxc", } Here's how I'm doing it right now: var result={}; var keys=Object.keys(kmap); for(var i=0;i<incoming.length;i++) { result[keys[i]]=incoming[i].value; } How do I do it using lodash or underscore . Built-in method would be even better,

Javascript Nested object to multidimensional array recursive function

青春壹個敷衍的年華 提交于 2019-12-25 16:46:11
问题 Ok here's one at which I've been scratching my head at without great success so far; Sorry in advance for the very long question... I am using this Lucene Query Parser to parse a string/query which produce this kind of data structure: // Notice that the repetition of 'field3' is on purpose Sample String: field1:val1 AND field2:val2 OR field3:val3 AND field3:val4 Result: { left: { field: "field1", term: "val1" }, operator: "AND" right: { left: { field: "field2", term: "val2" }, operator: "OR"

Unique array of objects and merge duplications using lodash or underscore

吃可爱长大的小学妹 提交于 2019-12-25 09:31:27
问题 I have an array of objects that can contain duplicates, but I'd like uniquify the array by a property, and then I want to merge properties of the duplicates conditionally. For example, give the array below var array = [ { name: 'object1', propertyA: true, propertyB: false, propertyC: false }, { name: 'object2', propertyA: false, propertyB: true, propertyC: false }, { name: 'object1', propertyA: false, propertyB: true, propertyC: false } ] Knowing each object has name, propertyA, propertyB,

Generate Array of Objects of different Objects Javascript

喜你入骨 提交于 2019-12-25 09:27:54
问题 I have severel Objects containing one sort of data: Prices: 'btc-usd' : 2640, 'ltc-usd': 40, ... Amount of Crypto: 'btc-usd': 2.533, 'ltc-usd': 10.42, ... How can I take these Objects and create an Array of Objects like: [ { name: 'Bitcoin', amount: 2.533, value: 2640, id: 'btc-usd' }, { name: 'Litecoin', amount: 10.42, value: 40, id: 'ltc-usd' }, ... ] Thanks a lot for your help! 回答1: You could map the keys of one of the objects to produce a new array of objects. You just have to make sure,