lodash

How to merge/concatenate values of same object properties in an array of objects using lodash?

[亡魂溺海] 提交于 2021-02-04 16:11:28
问题 I have an array of arrays of objects which looks like this: let fruitSamples = [ [ {'id': 1,'type': 'apples','samples': [1, 2, 3]}, {'id': 2,'type': 'bananas','samples': [1, 2, 7]}, {'id': 3,'type': 'pears','samples': [1, 2, 3]} ], [ {'id': 1,'type': 'apples','samples': [5, 2, 9]}, {'id': 2,'type': 'bananas','samples': [1, 7, 7]}, {'id': 3,'type': 'pears','samples': [12, 21, 32]} ], [ {'id': 1,'type': 'apples','samples': [11, 2, 33]}, {'id': 2,'type': 'bananas','samples': [17, 2, 67]}, {'id':

Remove items from one array of objects from another array of objects with _.differenceBy

余生颓废 提交于 2021-02-04 08:18:05
问题 I have two arrays of objects: var defendantList = [ { label: "Joe BLow" value: "Joe Blow" }, { label: "Sam Snead" value: "Sam Snead" }, { label: "John Smith" value: "John Smith" }, ]; var dismissedDefendants = [ { date: 'someDateString', value: "Joe Blow" }, { date: "someOtherDateString", value: "Sam Snead" } ]; I need to create an array that has values from defendantList that are not contained in dismissedDefendants. How can I do that simply, either with lodash or a standard JS array

Remove items from one array of objects from another array of objects with _.differenceBy

别说谁变了你拦得住时间么 提交于 2021-02-04 08:18:02
问题 I have two arrays of objects: var defendantList = [ { label: "Joe BLow" value: "Joe Blow" }, { label: "Sam Snead" value: "Sam Snead" }, { label: "John Smith" value: "John Smith" }, ]; var dismissedDefendants = [ { date: 'someDateString', value: "Joe Blow" }, { date: "someOtherDateString", value: "Sam Snead" } ]; I need to create an array that has values from defendantList that are not contained in dismissedDefendants. How can I do that simply, either with lodash or a standard JS array

How to filter array by comparing two arrays of objects with different elements in their objects?

旧巷老猫 提交于 2021-01-29 10:49:07
问题 How to filter array by comparing two arrays of objects with different elements in their objects? I have: arr1 =[{ x: 1, y: 2, z:3 }, { x: 2, y: 1, z:4 }]; arr2 = [{ x: 1, y: 2, a:5 }, { x: 2, y: 3, a:4 }]; I want to compare x and y values from both arrays and return the not macthing object from first array, in the above example return [{ x: 2, y: 1, z:4 }] I tried to use _.differenceWith(arr1, arr2, _.isEqual); but obviously for this the arrays should have similar objects which is not my case

How to iterate through every object.child in a deeply nested object?

穿精又带淫゛_ 提交于 2021-01-29 09:32:05
问题 I have following structure: instance: { children: [instance, instance ...] } As you can see each instance has children array with another instances in it, hence this repetition can go relatively deep until last nested child is reached. I need to iterate through each child of uppermost / given instance and perform a simple if condition. But I can't figure out how to do that as length of children can vary, plus we need to check children of each child and so on... 回答1: You can recursively

Filter objects in array by key-value pairs

余生颓废 提交于 2021-01-29 08:12:05
问题 I have an array of objects like so: [ { id: 'a', name: 'Alan', age: 10 }, { id: 'ab' name: 'alanis', age: 15 }, { id: 'b', name: 'Alex', age: 13 } ] I need to pass an object like this { id: 'a', name: 'al' } so that it does a wildcard filter and returns an array with the first two objects. So, the steps are: For each object in the array, filter the relevant keys from the given filter object For each key, check if the value starts with matching filter object key's value At the moment I'm using

Update/add/remove object in array if exist in other array by id

放肆的年华 提交于 2021-01-29 07:09:19
问题 I have in my angular app an array of items (ordered array of objects). Those items are getting updated and the new values are coming from a service (out of order). I need to write an algorithm to update the existing array. I used to just replace the old array with the new one, but this messed up the order of items. I have the following edge cases, where oldArr is my old array of items, the newArr is the array comming from service and the output , how the oldArray should look like: const

lodash pick in nested array

本秂侑毒 提交于 2021-01-27 17:18:01
问题 I have an array of objects like this: { "sizes":{ "thumbnail":{ "height":300, "width":300, "url":"http://example.com/wp-content/uploads/2017/04/web-300x300.jpg", "orientation":"landscape" }, "medium":{ "height":267, "width":400, "url":"http://example.com/wp-content/uploads/2017/04/web-400x267.jpg", "orientation":"landscape" }, "large":{ "height":441, "width":660, "url":"http://example.com/wp-content/uploads/2017/04/web-1024x684.jpg", "orientation":"landscape" }, "full":{ "url":"http://example

How to generate random hex string in javascript

白昼怎懂夜的黑 提交于 2021-01-22 05:16:06
问题 How to generate a random string containing only hex characters (0123456789abcdef) of a given length? 回答1: Short alternative using spread operator and .map() Demo 1 const genRanHex = size => [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join(''); console.log(genRanHex(6)); console.log(genRanHex(12)); console.log(genRanHex(3)); Pass in a number ( size ) for the length of the returned string. Define an empty array ( result ) and an array of strings in the range of [0-9]

Lodash debounce with React Input

半世苍凉 提交于 2021-01-20 17:17:27
问题 I'm trying to add debouncing with lodash to a search function, called from an input onChange event. The code below generates a type error 'function is expected', which I understand because lodash is expecting a function. What is the right way to do this and can it be done all inline? I have tried nearly every example thus far on SO to no avail. search(e){ let str = e.target.value; debounce(this.props.relay.setVariables({ query: str }), 500); }, 回答1: The debounce function can be passed inline