Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Last active May 31, 2017 23:53
Show Gist options
  • Save Woodsphreaker/321c0c80399f7e943bb4ca05979553d5 to your computer and use it in GitHub Desktop.
Save Woodsphreaker/321c0c80399f7e943bb4ca05979553d5 to your computer and use it in GitHub Desktop.
multipleObjectsValuesReduce
const arr1 = [
{ val1_1: 1, val1_2: 2, val1_3: 3, val1_4: 4 },
{ val1_5: 5, val1_6: 6, val1_7: 7, val1_8: 8 }
];
const arr2 = [
{ val2_1: 1, val2_2: 2, val2_3: 3, val2_4: 4 },
{ val2_5: 5, val2_6: 6, val2_7: 7, val2_8: 8 }
].reverse();
const arr3 = [
{ val2_1: 1, val2_2: 2, val2_3: 3, val2_4: 4 },
{ val2_5: 5, val2_6: 6, val2_7: 7, val2_8: 8 }
];
const arr4 = [
{ val2_1: 1, val2_2: 2, val2_3: 3, val2_4: 4 },
{ val2_5: 5, val2_6: 6, val2_7: 7, val2_8: 8 }
];
const times = (x, y) => x * y
const plus = (x, y) => x + y
const flattenObj = (arr) => [].concat(...arr.map(obj => Object.values(obj)))
const getOnlyObjectValues = (...objArr) => objArr.map(obj => flattenObj(obj))
const timesAll = (acc, cur) => acc.map((num, index) => times(cur[index] ,num))
const process = (...objArr) => {
const solve = getOnlyObjectValues(...objArr).reduce(timesAll)
.reduce(plus)
return solve
}
const calc = process(arr1, arr2, arr3)
console.log(calc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment