Skip to content

Instantly share code, notes, and snippets.

View isholaomotayo's full-sized avatar

Omotayo Ishola isholaomotayo

View GitHub Profile
@isholaomotayo
isholaomotayo / reduce-example.js
Created March 5, 2018 11:54 — forked from benwells/reduce-example.js
Using Array.reduce to sum a property in an array of objects
var accounts = [
{ name: 'James Brown', msgCount: 123 },
{ name: 'Stevie Wonder', msgCount: 22 },
{ name: 'Sly Stone', msgCount: 16 },
{ name: 'Otis Redding', msgCount: 300 } // Otis has the most messages
];
// get sum of msgCount prop across all objects in array
var msgTotal = accounts.reduce(function(prev, cur) {
return prev + cur.msgCount;
@isholaomotayo
isholaomotayo / gist:56b6c00ad11a0d04f7779ba82b7c10f6
Created November 2, 2017 14:36 — forked from solenoid/gist:1372386
javascript ObjectId generator
var mongoObjectId = function () {
var timestamp = (new Date().getTime() / 1000 | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
return (Math.random() * 16 | 0).toString(16);
}).toLowerCase();
};