const arr1 = [30, 300, 25, 75];
const arr2 = [50, 125, 500, 10];
let reducer = (accumulator, value) => accumulator + value;
function balance(arr1, arr2){
let arr = arr1.concat(arr2).sort((a, b) => a > b ? -1 : 1);
arr1 = [],
arr2 = [];
arr.map(value => {
arr1.reduce(reducer, 0) <= arr2.reduce(reducer, 0) ? arr1.push(value) : arr2.push(value);
});
return {
arr1: arr1,
arr2: arr2
}
}
console.log(balance(arr1, arr2));
// [500, 50, 10]
// [300, 125, 75, 30, 25]