Last active
August 13, 2020 06:52
-
-
Save OliverMensahDev/cb61471e2f27e57648bd6d2304f05dbb to your computer and use it in GitHub Desktop.
Declarative methods on array data collection
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 students = [ | |
{ | |
name: 'Oliver Mensah', | |
email: '[email protected]', | |
}, | |
{ | |
name: 'Olivia Mansah', | |
email: '[email protected]', | |
}, | |
]; | |
//Declarative | |
const emails = students.map((student) => student.email); | |
// console.log(emails); | |
//filter | |
const oliviaData = students.filter( | |
(student) => student.name === 'Olivia Mansah' | |
); | |
// console.log(oliviaData); | |
//reduce | |
const combinedEmails = students.reduce( | |
(acc, student) => acc + student.email, | |
'' | |
); | |
console.log(combinedEmails); | |
//creating declarative methods | |
//imp map | |
function impMap(data) { | |
const studentEmails = []; | |
for (student of students) { | |
studentEmails.push(student.email); | |
} | |
return studentEmails; | |
} | |
console.log(impMap(students)); | |
function impFilter(data) { | |
const oliviaData = []; | |
for (student of students) { | |
if (student.name === 'Olivia Mansah') { | |
oliviaData.push(student); | |
} | |
} | |
return oliviaData; | |
} | |
console.log(impFilter(students)); | |
function impReduce(data) { | |
let combinedEmails = ''; | |
for (student of students) { | |
combinedEmails += student.email; | |
} | |
return combinedEmails; | |
} | |
console.log(impReduce(students)); | |
function map(data, func) { | |
const result = []; | |
for (el of data) { | |
result.push(func(el)); | |
} | |
return result; | |
} | |
studentEmails = map(students, (student) => student.email); | |
console.log(studentEmails); | |
function filter(data, func) { | |
const result = []; | |
for (el of data) { | |
if (func(el)) result.push(el); | |
} | |
return result; | |
} | |
console.log(filter(students, (student) => student.name === 'Olivia Mansah')); | |
function reduce(data, func, initialValue) { | |
let acc = initialValue; | |
for (el of data) { | |
acc = func(acc, el); | |
} | |
return acc; | |
} | |
console.log(reduce(students, (acc, student) => acc + student.email, '')); | |
const shoppingCart = [ | |
{ product: 'Banana', unit_price: 79, quantity: 3 }, | |
{ product: 'Milk', unit_price: 499, quantity: 1 }, | |
{ product: 'Cream', unit_price: 599, quantity: 2 }, | |
{ product: 'Sugar', unit_price: 249, quantity: 1 }, | |
{ product: 'Apple', unit_price: 76, quantity: 6 }, | |
{ product: 'Bread', unit_price: 229, quantity: 2 }, | |
]; | |
/* | |
* Write a collection pipeline that calculates the total price of | |
* all the items in this shopping cart. | |
* | |
* Do not use any loops, if statements, or ternary operators. | |
* | |
* Good luck! | |
* | |
* $totalPrice = $shoppingCart.... | |
*/ | |
const sum = shoppingCart.reduce( | |
(acc, item) => acc + item.unit_price * item.quantity, | |
0 | |
); | |
console.log(sum); | |
const employees = [ | |
{ | |
name: 'John', | |
email: '[email protected]', | |
sales: [ | |
{ customer: 'The Blue Rabbit Company', order_total: 7444 }, | |
{ customer: 'Black Melon', order_total: 1445 }, | |
{ customer: 'Yellow Cake', order_total: 700 }, | |
], | |
}, | |
{ | |
name: 'Jane', | |
email: '[email protected]', | |
sales: [ | |
{ customer: 'The Grey Apple Company', order_total: 203 }, | |
{ customer: 'Yellow Cake', order_total: 8730 }, | |
{ customer: 'The Blue Rabbit Company', order_total: 3337 }, | |
{ customer: 'Green Mobile', order_total: 5310 }, | |
], | |
}, | |
{ | |
name: 'Dave', | |
email: '[email protected]', | |
sales: [ | |
{ customer: 'The Acute Toaster Company', order_total: 1091 }, | |
{ customer: 'Green Mobile', order_total: 2370 }, | |
], | |
}, | |
{ | |
name: 'Dana', | |
email: '[email protected]', | |
sales: [ | |
{ customer: 'Green Mobile', order_total: 203 }, | |
{ customer: 'Yellow Cake', order_total: 8730 }, | |
{ customer: 'The Piping Bull Company', order_total: 3337 }, | |
{ customer: 'The Cloudy Dog Company', order_total: 5310 }, | |
], | |
}, | |
{ | |
name: 'Beth', | |
email: '[email protected]', | |
sales: [ | |
{ customer: 'The Grey Apple Company', order_total: 1091 }, | |
{ customer: 'Green Mobile', order_total: 2370 }, | |
], | |
}, | |
]; | |
/* | |
Write a collection pipeline to find the customer whose combined | |
* total order value is the highest. | |
* | |
* Do not use any loops, if statements, or ternary operators. | |
* | |
* Good luck! | |
* | |
* mostValuableCustomer = employees->... | |
*/ | |
max = employees | |
.reduce(function (acc, cur) { | |
return [...acc, ...cur['sales']]; | |
}, []) | |
.reduce(function (prev, current) { | |
return prev['order_total'] > current['order_total'] ? prev : current; | |
}); | |
console.log(max['customer']); | |
//PHP | |
$employees | |
->pluck('sales') | |
->flatten(1) | |
->groupBy('customer') | |
->map(function ($groupedSales, $customer) { | |
return $groupedSales->sum('order_total'); | |
}) | |
->sort() | |
->reverse() | |
->keys() | |
->first(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment