lodash

Deep Flatten JavaScript Object Recursively

五迷三道 提交于 2019-12-03 22:38:40
Data : var data = [ { "id": 1, "level": "1", "text": "Sammy", "type": "Item", "items": [ { "id": 11, "level": "2", "text": "Table", "type": "Item", "items": [ { "id": 111, "level": "3", "text": "Dog", "type": "Item", "items": null }, { "id": 112, "level": "3", "text": "Cat", "type": "Item", "items": null } ] }, { "id": 12, "level": "2", "text": "Chair", "type": "Item", "items": [ { "id": 121, "level": "3", "text": "Dog", "type": "Item", "items": null }, { "id": 122, "level": "3", "text": "Cat", "type": "Item", "items": null } ] } ] }, { "id": 2, "level": "1", "text": "Sundy", "type": "Item",

Filtering multiple value with multiple key in json array using lodash

徘徊边缘 提交于 2019-12-03 22:17:18
i have a dynamic data each time there could be dynamically different key value pair to be filtered in different data. how can we filter it with multiple key,value in lodash. i was using its filter function but result is not achievable. example data: var data = [ { "VOTER" : 1012, "PARTY" : "REPUBLICAN", "PRECINCT" : 2408, "AGE_GROUP" : "71 +", "LAST_VOTED" : "08/2006", "YEARS_REG" : 51, "BALLOT_STATUS" : "PERM" }, { "VOTER" : 1013, "PARTY" : "REPUBLICAN", "PRECINCT" : 2411, "AGE_GROUP" : "71 +", "LAST_VOTED" : "08/2006", "YEARS_REG" : 50, "BALLOT_STATUS" : "PERM" }, { "VOTER" : 1014, "PARTY" :

How can I access iteration index in Ramda.map

扶醉桌前 提交于 2019-12-03 22:07:37
I used to write something like _.map(items, (item, index) => {}); with lodash. Usually I don't need index but sometimes it's useful. I'm migrating to Ramda now: R.map((item, index) => {}, items); index is undefined . Sure, I can create variable index in upper scope and increment it every time in map body but it's kinda wrong from FP point of view that Ramda stands for. So is there's any build in way of getting iteration index? Check out addIndex : Creates a new list iteration function from an existing one by adding two new parameters to its callback function: the current index, and the entire

Lo-Dash, difference between array and collection

拈花ヽ惹草 提交于 2019-12-03 22:05:58
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: collection (Array|Object|string): The collection to iterate over. Also, interestingly, there's a

Remap properties name and values using lodash

假如想象 提交于 2019-12-03 20:07:12
问题 I have this array: aItems = [{ "PropertyA": "apple", "PropertyB": "banana", "PropertyC": "dog", "PropertyD": "hotdog", "PropertyE": "coldcat", "PropertyF": "Y", "PropertyG": "N" }, ..., { "PropertyA": "this", "PropertyB": "is", "PropertyC": "json", "PropertyD": "code", "PropertyE": "wow", "PropertyF": "N", "PropertyG": "N" }] I would like use lodash to obtain this result: aItems = [{ "propertyA": "apple", "propertyB": "banana", "propertyC": "dog", "propertyD": "hotdog", "propertyE": "coldcat"

Filter array of objects whose any properties contains a value

冷暖自知 提交于 2019-12-03 19:11:10
问题 I'm wondering what is the cleanest way, better way to filter an array of objects depending on a string keyword . The search has to be made in any properties of the object. When I type lea I want to go trough all the objects and all their properties to return the objects that contain lea When I type italy I want to go trough all the objects and all their properties to return the objects that contain italy . I know there are lot of solutions but so far I just saw some for which you need to

transform object to array with lodash

流过昼夜 提交于 2019-12-03 18:24:23
问题 How can I transform a big object to array with lodash? var obj = { 22: {name:"John", id:22, friends:[5,31,55], works:{books:[], films:[],} 12: {name:"Ivan", id:12, friends:[2,44,12], works:{books:[], films:[],} } // transform to var arr = [{name:"John", id:22...},{name:"Ivan", id:12...}] 回答1: You can do var arr = _.values(obj); For documentation see here. 回答2: _.toArray(obj); Outputs as: [ { "name": "Ivan", "id": 12, "friends": [ 2, 44, 12 ], "works": { "books": [], "films": [] } }, { "name":

Filter array of objects with another array of object

被刻印的时光 ゝ 提交于 2019-12-03 17:09:52
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', category: 'Office', skill: 'Advanced', color: 'blue', price: 260.00 }, { id: 5, name: 'Product 5',

lodash filter on key with multiple values

送分小仙女□ 提交于 2019-12-03 15:16:30
I am trying to find out in lodash javascript library , how to find out filter array of objects multiple values of key. something similar to SQL - WHERE KEY in (val1, val2) Having said, with following example : var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'Avding', 'age': 34, 'active': true } ]; _.filter(...) How can I find users, who's age in (34, 36) ?? numbers can also change runtime Lodash's filter accepts a predicate. You can create the predicate using partial application, so you can change the values easily: var

How can I use lodash/underscore to sort by multiple nested fields?

安稳与你 提交于 2019-12-03 14:38:18
问题 I want to do something like this: var data = [ { sortData: {a: 'a', b: 2} }, { sortData: {a: 'a', b: 1} }, { sortData: {a: 'b', b: 5} }, { sortData: {a: 'a', b: 3} } ]; data = _.sortBy(data, ["sortData.a", "sortData.b"]); _.map(data, function(element) {console.log(element.sortData.a + " " + element.sortData.b);}); And have it output this: "a 1" "a 2" "a 3" "b 5" Unfortunately, this doesn't work and the array remains sorted in its original form. This would work if the fields weren't nested