Skip to content

Instantly share code, notes, and snippets.

View Array remove objects with same prop value using reduce and sort
function deDupeArrByPropReduceSort(objectArray, prop) {
//copy the parameter array as we don't want to mutate that object
const sorted = Object.assign([], objectArray);
// Now we sort the new array by the prop value
sorted.sort(function(a, b){
if (a[prop] > b[prop]) {
return -1;
} else {
return 1;
}
@jessekinsman
jessekinsman / Remove objects with duplicate prop value in array
Last active Mar 28, 2019
Returns a new array with objects that have a unique value by the prop that is passed to it.
View Remove objects with duplicate prop value in array
// Prop is the prop you want to check in the array for a unique value
// objectArray is the original array of objects
function deDupeArrByPropReduce(objectArray, prop) {
return objectArray.reduce(function (acc, obj, ind, arr) {
// acc is the accumulator. On line 14 we pass an emtpy array as the inital value
if (acc.findIndex(obj1 => {
// We check if the accumulator has an object with the same value as the iterated obj.
return obj1[prop] === obj[prop];
}) == -1) {
// if the findIndex function returns -1 we add the object to the accumulator
View Array with objects that have duplicate prop values
const people = [
{ name: 'Alice', age: 21 },
{ name: 'Max', age: 20 },
{ name: 'Jane', age: 20 },
{ name: 'Jesse', age: 22 },
{ name: 'Mike', age: 24 },
{ name: 'Jen', age: 21 },
{ name: 'Jesse', age: 22 },
{ name: 'Jen', age: 21 },
{ name: 'Mike', age: 25 },
@jessekinsman
jessekinsman / gradient.scss
Created Feb 4, 2015
Sass Linear Gradient Mixin
View gradient.scss
// should support most browsers with a fallback for no gradient support
@mixin gradient($from, $to) {
background: $from; /* Old browsers */
background: -moz-linear-gradient(top, $from 0%, $to 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,$from), color-stop(100%,$to)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, $from 0%,$to 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, $from 0%,$to 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, $from 0%,$to 100%); /* IE10+ */
background: linear-gradient(to bottom, $from 0%,$to 100%); /* W3C */
@jessekinsman
jessekinsman / grunt.js
Last active Aug 29, 2015
Grunt configuration for using autoprefixer with Zurb Foundation
View grunt.js
// After some debugging, got this working. Didn't see it documented anywhere so thought I would put this up to help any others
// First install the grunt-postcss and autoprefixer-core npm repositiories
// Looks like this: npm install grunt-postcss autoprefixer-core
var autoprefixer = require('autoprefixer-core');
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
@jessekinsman
jessekinsman / gist:306a7c53a7cfe2bb0797
Created Jan 24, 2015
Javascript function to check if time has elapsed between two dates
View gist:306a7c53a7cfe2bb0797
// lastUpdate param should be a date object or a number representing milliseconds since 1970
// interval param should be seconds
// Interval is the elapsed time you are checking
// Returns true if the interval time has elapsed
// Returns false if the interval has not elapsed
function update(lastUpdate, interval) {
var curTime = new Date().getTime();
lastUpdate = (typeof lastUpdate.getTime == "function")? lastUpdate.getTime(): lastUpdate;
interval *= 1000;
@jessekinsman
jessekinsman / gist:6cc3ba1eb3667991299f
Last active Aug 29, 2015
Backbone add models to collection (Titanium Alloy or any Backbone Collection)
View gist:6cc3ba1eb3667991299f
// Often you want to dump a large amount of models into a collection at one time.
// This take models from one collection and adds thems to another
// This assumes you have two collections already initialized with models.
// If you had one collection that had change events wired to render a view you might use this.
collectionAttachedToView.add(collectionToAdd.models);
//This would work, however, it will fire an add event everytime one of the models is added.
// This will slow your app down ad it will try to render over and over again as many times as the models you are adding
// To avoid this, use the silent option
@jessekinsman
jessekinsman / gist:9962e33f497613320695
Created Jan 24, 2015
Padding a one digit number in javascript
View gist:9962e33f497613320695
// One Digit returns string
function pad (n) {
return ("0" + n.toString()).slice(-2);
}
//Two digit returns string
function pad (n) {
return ("00" + n.toString()).slice(-3);
}
You can’t perform that action at this time.