lodash

Lodash: filter a nested object by multiple properties

那年仲夏 提交于 2020-01-03 04:34:37
问题 Consider the following example: var products = { "Products": [{ "Title": "A", "Categories": [{ "Name": "Type", "Properties": ["Type 1", "Type 2", "Type 3"] }, { "Name": "Market", "Properties": ["Market 1", "Market 2", "Market 3", "Market 4"] }, { "Name": "Technology", "Properties": ["Tech 1", "Tech 2"] }] }, { "Title": "B", "Categories": [{ "Name": "Type", "Properties": ["Type 1", "Type 3"] }, { "Name": "Market", "Properties": "Market 1" }, { "Name": "Technology", "Properties": ["Tech 1",

using lodash unique on an array of arrays

百般思念 提交于 2020-01-03 04:00:26
问题 I am not sure how to use this to remove duplicate arrays from the main array. so consider the following: var arr = [ [0, 1], [0, 1], [2, 1], ]; The resulting array should look like: var arr = [ [0, 1], [2, 1], ]; The documentation makes sense for a single array of elements but not a 2d array. Ideas? Update, One small issue with some solutions: The array to be turned unique might have: var arr = [ [0, 1] [0, 2] [0, 3] [0, 1] [2, 1] ] the array in side the array should have all value compared,

VueJS display grouped array of objects

青春壹個敷衍的年華 提交于 2020-01-03 02:29:05
问题 I'm fetching a list of countries from the API and I want to display them in groups by subregion (continent). Like that: API gives me a response which is an array of objects (countries) and for each object there is a key called 'subregion' - I want to group by that key. I use lodash for grouping, but maybe there is a Vue method I'm not familiar with. My JS code: var app = new Vue({ el: '#app', data: { countries: null, }, created: function () { this.fetchData(); }, methods: { fetchData:

flatten javascript array of objects

这一生的挚爱 提交于 2020-01-02 22:42:36
问题 I have an array of object with hierarchical structure, something like this: [ {name: 'ParentOne', children: [ {name: 'ParentOneChildOne'}, {name: 'ParentOneChildTwo', children: [ {name: 'ParentOneChildTwoGrandChildOne'}, ]}, ]}, {name: 'ParentTwo', children: [ {name: 'ParentTwoChildOne', children: [ {name: 'ParentTwoChildOneGrandChildOne'}, {name: 'ParentTwoChildOneGrandChildTwo'} ]}, {name: 'ParentTwoChildTwo'} ]} ]; I want to flatten it: [ {name: 'ParentOne'}, {name: 'ParentOneChildOne'},

flatten javascript array of objects

怎甘沉沦 提交于 2020-01-02 22:42:10
问题 I have an array of object with hierarchical structure, something like this: [ {name: 'ParentOne', children: [ {name: 'ParentOneChildOne'}, {name: 'ParentOneChildTwo', children: [ {name: 'ParentOneChildTwoGrandChildOne'}, ]}, ]}, {name: 'ParentTwo', children: [ {name: 'ParentTwoChildOne', children: [ {name: 'ParentTwoChildOneGrandChildOne'}, {name: 'ParentTwoChildOneGrandChildTwo'} ]}, {name: 'ParentTwoChildTwo'} ]} ]; I want to flatten it: [ {name: 'ParentOne'}, {name: 'ParentOneChildOne'},

flatten javascript array of objects

岁酱吖の 提交于 2020-01-02 22:42:08
问题 I have an array of object with hierarchical structure, something like this: [ {name: 'ParentOne', children: [ {name: 'ParentOneChildOne'}, {name: 'ParentOneChildTwo', children: [ {name: 'ParentOneChildTwoGrandChildOne'}, ]}, ]}, {name: 'ParentTwo', children: [ {name: 'ParentTwoChildOne', children: [ {name: 'ParentTwoChildOneGrandChildOne'}, {name: 'ParentTwoChildOneGrandChildTwo'} ]}, {name: 'ParentTwoChildTwo'} ]} ]; I want to flatten it: [ {name: 'ParentOne'}, {name: 'ParentOneChildOne'},

Convert array of objects to object of arrays using lodash

為{幸葍}努か 提交于 2020-01-02 11:14:04
问题 The title is a little bit confusing but I essentially want to convert this: [ {a: 1, b: 2, c:3}, {a: 4, b: 5, c:6}, {a: 7, b: 8, c:9} ] into: { a: [1,4,7], b: [2,5,8], c: [3,6,9] } using lodash (requirement). Any ideas??? 回答1: Here's a solution using lodash that maps across the keys and plucks the values for each key from the data before finally using _.zipOobject to build the result. var keys = _.keys(data[0]); var result = _.zipObject(keys, _.map(keys, key => _.map(data, key))); 回答2: Look

Merging objects containing same key/value with LoDash

送分小仙女□ 提交于 2020-01-02 10:03:40
问题 After doing some operations with LoDash from an initial array, I end up with the following: [ {number:3, product:'apple'}, {number:2, product:'apple'}, {number:4, product:'pear'} ] How could I manipulate it to end up with: [ {number:5, product:'apple'}, {number:4, product:'pear'} ] This is the original code, just in case: $scope.data = [ { date:'date', products: [ { number: 3, product: 'Apple' }, { number: 4, product: 'Pear' } ] }, { date:'date', products: [ { number: 2, product: 'Apple' } ]

How to replace an object in object array in javascript (lodash)

混江龙づ霸主 提交于 2020-01-02 06:12:16
问题 I have following object array: var arr = [ { id : "a1", guid : "sdfsfd", ... value : "abc", status: false }, { id : "a2", guid : "sdfsfd", ... value : "def", status: true }, ... ] I have this object: var obj = { id : "a1", guid : "sdfsfd", ... value : "xyz", status : true } I need to replace the object in the array with this object where the "id" is same. So the resulting array will be: var arr = [ { id : "a1", guid : "sdfsfd", ... value : "xyz", status: true }, { id : "a2", guid : "sdfsfd",

Is it possible to “filter” a Map by value in Typescript?

六眼飞鱼酱① 提交于 2020-01-02 03:51:09
问题 I am looking for a way to filter a map by value instead of key. I have a data set that is modeled as follows in my Angular application: { "85d55e6b-f4bf-47bb-a988-78fdb9650ef0": { is_deleted: false, public_email: "007@example.org", id: "85d55e6b-f4bf-47bb-a988-78fdb9650ef0", modified_at: "2017-09-26T15:35:06.853492Z", social_url: "https://facebook.com/jamesbond007", event_id: "213b01de-da9e-4d19-8e9c-c0dae63e019c", visitor_id: "c3c232ff-1381-4776-a7f2-46c177ecde1c", }, } The keys on these