Last active
April 2, 2017 20:14
-
-
Save Sudmota/0410ff6861e432297a2deab7c01aca7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mergeSortObj = (arr, param) => { | |
if (arr.length < 2) | |
return arr; | |
const middle = parseInt(arr.length / 2); | |
const [left, right] = [arr.slice(0, middle), arr.slice(middle, arr.length)] | |
return mergeObj(mergeSortObj(left, param), mergeSortObj(right, param), param); | |
} | |
const mergeObj = (leftArr, rightArr, param) => { | |
const result = []; | |
while (leftArr.length && rightArr.length) { | |
if (leftArr[0][param] <= rightArr[0][param]) { | |
result.push(leftArr.shift()); | |
} else { | |
result.push(rightArr.shift()); | |
} | |
} | |
while (leftArr.length) | |
result.push(leftArr.shift()); | |
while (rightArr.length) | |
result.push(rightArr.shift()); | |
return result; | |
} | |
const avgObj = (arr, param) => { | |
const length = arr.length | |
return arr.reduce((acc, val) => { | |
return acc + val[param] / length; | |
}, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment