Array of object deep comparison with lodash

You can make use of differenceWith() with an isEqual() comparator, and invoke isEmpty to check if they are equal or not. var isArrayEqual = function(x, y) { return _(x).differenceWith(y, _.isEqual).isEmpty(); }; var result1 = isArrayEqual( [{a:1, b:2}, {c:3, d:4}], [{b:2, a:1}, {d:4, c:3}] ); var result2 = isArrayEqual( [{a:1, b:2, c: 1}, {c:3, d:4}], [{b:2, … Read more

Update if exists or add new element to array of objects – elegant way in javascript + lodash

In your first approach, no need for Lodash thanks to findIndex(): function upsert(array, element) { // (1) const i = array.findIndex(_element => _element.id === element.id); if (i > -1) array[i] = element; // (2) else array.push(element); } Example: const array = [ {id: 0, name: ‘Apple’, description: ‘fruit’}, {id: 1, name: ‘Banana’, description: ‘fruit’}, {id: … Read more

Lodash: how do I use filter when I have nested Object?

lodash allows nested object definitions: _.filter(summary.data, {category: {parent: ‘Food’}}); As of v3.7.0, lodash also allows specifying object keys in strings: _.filter(summary.data, [‘category.parent’, ‘Food’]); Example code in JSFiddle: https://jsfiddle.net/6qLze9ub/ lodash also supports nesting with arrays; if you want to filter on one of the array items (for example, if category is an array): _.filter(summary.data, {category: [{parent: … Read more

Using lodash to compare jagged arrays (items existence without order)

If you sort the outer array, you can use _.isEqual() since the inner array is already sorted. var array1 = [[‘a’, ‘b’], [‘b’, ‘c’]]; var array2 = [[‘b’, ‘c’], [‘a’, ‘b’]]; _.isEqual(array1.sort(), array2.sort()); //true Note that .sort() will mutate the arrays. If that’s a problem for you, make a copy first using (for example) .slice() … Read more

How can I remove an element from a list, with lodash?

As lyyons pointed out in the comments, more idiomatic and lodashy way to do this would be to use _.remove, like this _.remove(obj.subTopics, { subTopicId: stToDelete }); Apart from that, you can pass a predicate function whose result will be used to determine if the current element has to be removed or not. _.remove(obj.subTopics, function(currentObject) … Read more

How to remove undefined and null values from an object using lodash?

You can simply chain _.omit() with _.isUndefined and _.isNull compositions, and get the result with lazy evaluation. Demo var result = _(my_object).omit(_.isUndefined).omit(_.isNull).value(); Update March 14, 2016: As mentioned by dylants in the comment section, you should use the _.omitBy() function since it uses a predicate instead of a property. You should use this for lodash … Read more

Lodash create collection from duplicate object keys

Use _.groupBy and then _.map the resulting object to an array of objects. var newOutput = _(output) .groupBy(‘article’) .map(function(v, k){ return { article: k, titles: _.map(v, ‘title’) } }) .value(); 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”}]; let newOutput = _(output) .groupBy(‘article’) .map(function(v, k){ return { article: k, titles: _.map(v, … Read more

tech