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"
      }
    ]
  }
] 
2
  • 1
    Can you share what you've tried so far? Commented Dec 5, 2013 at 22:57
  • Do you need help with the implementation or are you just checking if there's already something implemented? Commented Dec 5, 2013 at 23:00

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

3 Comments

This works great. I guess I'm still confused as to what's going on in that hash function.
It uses javascript prototypes. This may be an interesting read for you: stackoverflow.com/questions/572897/…
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.