Skip to content

Instantly share code, notes, and snippets.

View erkanzileli's full-sized avatar
:shipit:
what if

Erkan Zileli erkanzileli

:shipit:
what if
View GitHub Profile
@erkanzileli
erkanzileli / orderByKey.js
Created January 23, 2019 07:27
order an array by selected key and choose ascending or descending
function orderByKey (arr, key, asc) {
return arr.sort((a, b) => {
if (a[key] < b[key]) {
return asc === true ? -1 : 1
}
if (a[key] > b[key]) {
return asc === true ? 1 : -1
}
return 0
})
@erkanzileli
erkanzileli / customMergeObjectsWithSameKeys.js
Created January 22, 2019 10:11
Merge objects of the same key name in arrays
var customMergeObjectsWithSameKeys = function (arr, key, mergeFunction) {
var _arr = []
arr.forEach(item => {
var index = _arr.findIndex(_item => _item[key] === item[key])
if (index === -1) {
_arr.push(item)
} else {
_arr[index] = mergeFunction(_arr[index], item)
}
})