eph baum dot dev

← Back to blog

Array.prototype.diff()

Published on 06/29/2014 07:30 PM by Eph Baum

Update - 23 August 2014

A little mea culpa follow up here… I had to write a follow-up post that corrects something I missed here… check it out.

Original article continues

Sometimes in the day to day you find some problem that is just fun to try to solve without a Google (Stack Overflow) search.

Recently the problem I needed to solve was this:

I have a list of ‘feed’ items on a page where a long poll is watching for new ‘feed’ items to add to the DOM. In this case, really, all I want to do is highlight ANY feed item that is newly shown with a green highlight which then fades away.

In this case what I decided to do was ‘diff’ an array of ‘feed’ ids that have already been displayed with an array of ids that represents the items coming from the server. With a fixed number of items on both sides, 20, I know that I need to highlight anything that is new. It seemed to me that simply comparing the two arrays gives me the ids that I need to work with.

Here was my solution:

Array.prototype.diff = function( arr ) { 
  return this.filter( 
    function( val ) { 
      return arr.indexOf( val ) < 0; 
    }); 
};

Essentially what we’re doing here is taking an Array and filtering it with the value of an element of another Array and spitting a new Array back out that tells me exactly what is different.

By way of example:

var index = [ 14, 15, 16, 21, 28 ],
    newIndex = [ 15, 16, 21, 28, 29 ]; 
    
var diffIndex = index.diff( newIndex ); 

console.log( diffIndex ); 

> [ 29] 

var index2 = [ 1, 2, 3, '<3' ], 
    newIndex2 = [ 1, 2, 3 ]; 
    
diffIndex2 = index2.diff( newIndex2 ); 

console.log( diffIndex2 );

> ['<3']

Did my example have to log out as less than three? Of course it did.

Written by Eph Baum

← Back to blog
  • 50 Stars - Puzzle Solver (of Little Renown)

    50 Stars - Puzzle Solver (of Little Renown)

    Join Eph Baum as they recount their journey through the Advent of Code 2023. For the first time, Eph completes all puzzles, leveraging resources like GPT-4 and Code Llama. Despite the challenges and time constraints, Eph not only stays on top of the puzzles but also lands on the top 1,000 leaderboard. Dive into this post to explore the role of generative AIs in problem-solving and the joy of coding puzzles. - GitHub Co-pilot

  • Don't Trust AI - An Advent of Code Tale

    Don't Trust AI - An Advent of Code Tale

    Join Eph Baum in 'Don't Trust AI - An Advent of Code Tale' as they navigate the Advent of Code 2023. Despite the December rush, Eph is determined to complete all puzzles. This post shares an intriguing incident where an AI-generated code line proves less than helpful. Eph's journey underscores the importance of verifying AI suggestions, especially when optimizing code. Dive in to explore the challenges and triumphs of coding puzzles, and the role of AI in this process. - GitHub CoPilot

  • Condoning Another Pi Day

    Condoning Another Pi Day

    Placeholder description for imported post from Ghost Blog

  • ANSI Terminal Colors

    ANSI Terminal Colors

    Placeholder description for imported post from Ghost Blog

  • WTF is Idiomatic

    WTF is Idiomatic

    Placeholder description for imported post from Ghost Blog

  • From Early Return in OOP to Control Flow in Elixir - A Transition Guide

    From Early Return in OOP to Control Flow in Elixir - A Transition Guide

    Placeholder description for imported post from Ghost Blog