1

I have an array of objects, which has an array of 'tracks.' What I would like to do is compare the mbid values across all of the track objects, check if there are any duplicates, and store the duplicate track objects into a new array. Any help or guidance?

[
  {
    "track": [
      {
        "name": "Radiapathy",
        "mbid": "4c0767f1-1c2e-4790-a8d1-ee7f78f0ac84",
        "url": "http://www.last.fm/music/The+Velvet+Teen/_/Radiapathy"
      },
      {
        "name": "How Did I Get Here",
        "mbid": "64b3078f-89cd-4ad5-bc7a-b43af082b00f",
        "url": "http://www.last.fm/music/Odesza/_/How+Did+I+Get+Here"
      },
      {
        "name": "Sunshine Roof",
        "mbid": "837db975-c93e-45ca-992c-0c924ef0f34f",
        "url": "http://www.last.fm/music/The+Innocence+Mission/_/Sunshine+Roof"
      }
    ]
  },
  {
    "track": [
      {
        "name": "Traveling",
        "mbid": "b40c24b8-3295-4219-af59-855b69958ca2",
        "url": "http://www.last.fm/music/Tennis/_/Traveling"
      },
      {
        "name": "Ghost",
        "mbid": "6273ae8f-3d2c-44c6-8c0d-53013ba79b4e",
        "url": "http://www.last.fm/music/Neutral+Milk+Hotel/_/Ghost"
      },
      {
        "name": "Strange",
        "mbid": "5a015df2-6c4a-4192-bea8-14ec5f297713",
        "url": "http://www.last.fm/music/Built+to+Spill/_/Strange"
      }
    ]
  },
  {
    "track": [
      {
        "name": "Radiapathy",
        "mbid": "4c0767f1-1c2e-4790-a8d1-ee7f78f0ac84",
        "url": "http://www.last.fm/music/The+Velvet+Teen/_/Radiapathy"
      },
      {
        "name": "Let Me Show You Love",
        "mbid": "",
        "url": "http://www.last.fm/music/Cut+Copy/_/Let+Me+Show+You+Love"
      },
      {
        "name": "Footsteps",
        "mbid": "",
        "url": "http://www.last.fm/music/Cut+Copy/_/Footsteps"
      }
    ]
  }
] 
cusejuice
  • 10,285
  • 26
  • 90
  • 145

1 Answers1

3

The answer to this question (Elminating duplicates in a JSON object) pretty much answers yours. I've used this (the "Smart(er) way") and modified to suit your array & requirements:

var key = null;
var noDupes = [];
var dupes = [];
for (var i = 0; i < arr.length; i++)
{
    // loop through each track:
    for (var j = 0; j < arr[i].track.length; j++)
    {
        key = arr[i].track[j].mbid;
        if (!hash.contains(key))
        {
            hash.add(key);
            noDupes.push(arr[i].track[j]); // if not duplicate
        }
        else
        {
            dupes.push(arr[i].track[j]); // if duplicate
        }
    }
}

http://jsfiddle.net/6wspy/

Community
  • 1
  • 1
mnsr
  • 12,337
  • 4
  • 53
  • 79
  • This works great. I guess I'm still confused as to what's going on in that `hash` function. – cusejuice Dec 05 '13 at 23:51
  • It uses javascript prototypes. This may be an interesting read for you: http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work – mnsr Dec 06 '13 at 00:02
  • But as for the hash object, it stores each value that you're checking dupes for (in your case, mbid). So in the above example, hash has a "keys" object inside it which stores each "mbid" that's been added. So when you do a loop over the array, it checks to see if it's in the hash keys object, and adds it to either noDupes or dupes accordingly. Hope that makes sense... – mnsr Dec 06 '13 at 00:09