Mind Mining Medium

December 17, 2009

Pure JavaScript Flatten Array

Update: Paul Irish thought it would be grand to point out that my test case here isn’t the best. It’s good to note that the following functions work for all values in your array. I would suggest checking out the QUnit testing page to see other examples.

While working on my current project I needed a way to turn a heavily nested array and flatten it. There are some nice JavaScript libraries that have functions to do so, such as Underscore’s _.flatten and Prototype’s .flatten to name a few.

However, with reduce being implemented only in JavaScript 1.8 and not wanting to include a whole JavaScript library to do one task, I decided to write my own. You may want to take a look at the QUnit testing page.

Native Function

function flatten(array){
    var flat = [];
    for (var i = 0, l = array.length; i < l; i++){
        var type = Object.prototype.toString.call(array[i]).split(' ').pop().split(']').shift().toLowerCase();
        if (type) { flat = flat.concat(/^(array|collection|arguments|object)$/.test(type) ? flatten(array[i]) : array[i]); }
    return flat;


var given = [[1, [2, [3, [4, [5, [6, [7, [8, [9, [0]]]]]]]]]]];
var value = flatten(given);

// 'value' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Array Object Extend:

Array.prototype.flatten = function flatten(){
   var flat = [];
   for (var i = 0, l = this.length; i < l; i++){
       var type = Object.prototype.toString.call(this[i]).split(' ').pop().split(']').shift().toLowerCase();
       if (type) { flat = flat.concat(/^(array|collection|arguments|object)$/.test(type) ? flatten.call(this[i]) : this[i]); }
   return flat;


var given = [[1, [2, [3, [4, [5, [6, [7, [8, [9, [0]]]]]]]]]]];
var value = given.flatten();

// 'value' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
December 12, 2009

lala extremely flawed

A week ago I wrote about how I used web apps to feed into other web apps, more specifically I talked about lala and it’s “lala music mover”.

Recently a thought formed; while watching how lala’s music mover worked, I noticed that it wasn’t checking anything other than the ID3 tag. I ran a real quick experiment.

To test this out, I found a song that I didn’t already have. “Fireflies” by “Owl City” seemed a great fit. I made a clean iTunes library, created a sample mp3 and added it to iTunes. Then, I just edited the ID3 tag to match some basic information about the Fireflies track. Finally I made the lala mover rescan my library. Surprisingly when I checked lala, they had added it to my library.

What does this teach us? The lala mover does nothing but check for ID3 tags. But really, I just saved 10¢. It’s not possible to download the track from lala being that they think I already own the track.

December 08, 2009

Web Apps Feeding Web Apps

I just realized that I’ve been feeding web apps with other web apps, and all for free.

I listen to last.fm via sweet.fm which downloads my loved tracks to my iTunes; those are then matched or uploaded to lala.com’s catalog with their lala music mover app, and finally the tracks are scrobbled back to last.fm.

The cycle of last.fm > sweet.fm > iTunes > lala > last.fm keeps my library growing. Moreover, I get one free listen to new tracks on lala.

So after a week of using this process, I’ve added over two thousand songs to lala that I can listen to anywhere, anytime.