How to use the unionBy function from lodash

Find comprehensive JavaScript lodash.unionBy code examples handpicked from public code repositorys.

lodash.unionBy returns an array of unique values, computed by applying a function to each element of given arrays and comparing the results.

409
410
411
412
413
414
415
416
417
418
module.exports.truthy              = _.truthy;
module.exports.truthyAll           = _.truthyAll;
module.exports.unary               = _.unary;
module.exports.unescape            = _.unescape;
module.exports.union               = _.union;
module.exports.unionBy             = _.unionBy;
module.exports.unionWith           = _.unionWith;
module.exports.uniq                = _.uniq;
module.exports.uniqBy              = _.uniqBy;
module.exports.uniqWith            = _.uniqWith;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

153
154
155
156
157
158
159
160
161
162
163
164
165
console.log(takeWhile); // => [1, 2]


const union = _.union([2], [1, 2]);
console.log(union); // => [2, 1]


const unionBy = _.unionBy([2.1], [1.2, 2.3], Math.floor);
console.log(unionBy); // => [2.1, 1.2]


const unionWith = _.unionWith([2.1], [1.2, 2.3], (a, b) => Math.floor(a) === Math.floor(b));
console.log(unionWith); // => [2.1, 1.2]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.unionBy work?

lodash.unionBy is a method from the Lodash library that creates an array of unique values by merging multiple arrays and filtering duplicates using a provided iteratee function to generate a unique value for each element before comparing them.

68
69
70
71
72
73
74
75
76
77
            item2.num = item2.s_num
            item2.down_num = 0 //下架数量
        }

        result3 = _.union(result3, result4) //合并
        result3 = _.unionBy(result3, 'g_id') //完全去重
    });
})

//计算上架商品剩余数量
fork icon0
star icon2
watch icon1

358
359
360
361
362
363
364
365
366

  await sleep(200)
  
}
mainKeywordArray = mainKeywordArray.sort((a, b) =>  (b.monthlyPcQcCnt + b.monthlyMobileQcCnt) - (a.monthlyPcQcCnt + a.monthlyMobileQcCnt))
mainKeywordArray = _.unionBy(mainKeywordArray, "relKeyword")
.filter(item => item.monthlyPcQcCnt + item.monthlyPcQcCnt < 10000)
.filter((item, index) => index < 20)
.map(item => item.relKeyword)
fork icon0
star icon0
watch icon1

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const _ = require("lodash");

const arr1 = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
];
const arr2 = [
  { id: 2, name: "Jane" },
  { id: 3, name: "Bob" },
];

const result = _.unionBy(arr1, arr2, "id");

console.log(result);
// Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Bob' }]

In this example, lodash.unionBy is used to merge two arrays arr1 and arr2 while removing any duplicates that share a common property, in this case, id. The resulting array result contains all the unique objects from both arrays.

1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
if (Array.isArray(group) && Array.isArray(group[0])) {
  noDoubleNestedGroup();
  group = group.flat();
}

options.attributes = _.unionBy(
  options.attributes,
  group,
  [[this.sequelize.fn(aggregateFunction, aggregateColumn), aggregateFunction]],
  a => (Array.isArray(a) ? a[1] : a),
fork icon0
star icon0
watch icon420

+ 8 other calls in file

447
448
449
450
451
452
453
454
455
456
457
let union1 = _.union([2], [1, 2]);
console.log('union1--->', union1);
//union1---> [ 2, 1 ]


//_.unionBy([arrays],[iteratee=_.identity])
let unionBy1 = _.unionBy([2.1], [1.2, 2.3], Math.floor);
console.log('unionBy1--->', unionBy1);
//unionBy1---> [ 2.1, 1.2 ]
let unionBy2 = _.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
console.log('unionBy2--->', unionBy2);
fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)