2

Below is an examplle of an array I have

var mm =  [
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"IMG_0245.JPG","text":"IMG_0245.JPG","parent":"Junk"},
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"IMG_0246.JPG","text":"IMG_0246.JPG","parent":"Junk"},
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"IMG_0247.JPG","text":"IMG_0247.JPG","parent":"Junk"},
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"IMG_0248.JPG","text":"IMG_0248.JPG","parent":"Junk"},
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"IMG_0249.JPG","text":"IMG_0249.JPG","parent":"Junk"},
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"IMG_0250.JPG","text":"IMG_0250.JPG","parent":"Junk"},
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"IMG_0251.JPG","text":"IMG_0251.JPG","parent":"Junk"},
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"jingling.rar","text":"jingling.rar","parent":"Junk"},
    {"id":"Junk","text":"Junk","parent":"#"},
    {"id":"pics.rar","text":"pics.rar","parent":"Junk"}
];

If you notice, the first and third objects in the array are same and so on. I've looked up for built in functions but I couldnt find any. IS there any simple built in. simple function to do this in jQuery or Javascript?

2
  • Do you have the same ID to all objects or it was just a typo? Commented Feb 15, 2015 at 13:01
  • Yes, the ID of objects that are similar have to be removed. Commented Feb 15, 2015 at 13:07

2 Answers 2

1

You can do something like this:

Array.prototype.getUnique = function(){
   var o = {}, a = [];
   for(var i = 0, l = this.length; i < l; ++i){
      if(o.hasOwnProperty(JSON.stringify(this[i]))) {
         continue;
      }
      a.push(this[i]);
      o[JSON.stringify(this[i])] = 1;
   }
   return a;
}

This will return you an array with unique values only. I don't think there is any built-in function for it.

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

8 Comments

I was looking for a jQuery or javascript answer.But thanks anyway!
@SaiKrishnaDeep: This is plain JavaScript. To invoke: var cleanedArray = mm.getUnique(); cleanedArray will contain the array without doubles.
Have a look at this. jsfiddle.net/y9hb60qu Doesn't seem to work? I just get the first array.
@SaiKrishnaDeep I've examined this answer and corrected some of the problems: jsfiddle.net/y9hb60qu/3
The code snippet doesn't work. The function returns an array with only one element - the first one of the original array (unless you have other elements that are primitives). Here is why: object properties' names can only be strings (at least as of ECMAScript 5.1). You cannot have an object act as a key for a property, it will be converted to [object Object]. This way, the first if condition will fail, since there are no properties in the object yet, but each subsequent iteration will end up checking whether the object has the property called [object Object] and continue the loop.
|
0

If the ID of the objects is always the same you can do this:

var mm =  [  {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"IMG_0245.JPG","text":"IMG_0245.JPG","parent":"Junk"},
            {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"IMG_0246.JPG","text":"IMG_0246.JPG","parent":"Junk"},
            {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"IMG_0247.JPG","text":"IMG_0247.JPG","parent":"Junk"},
            {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"IMG_0248.JPG","text":"IMG_0248.JPG","parent":"Junk"},
            {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"IMG_0249.JPG","text":"IMG_0249.JPG","parent":"Junk"},
            {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"IMG_0250.JPG","text":"IMG_0250.JPG","parent":"Junk"},
            {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"IMG_0251.JPG","text":"IMG_0251.JPG","parent":"Junk"},
            {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"jingling.rar","text":"jingling.rar","parent":"Junk"},
            {"id":"Junk","text":"Junk","parent":"#"},
            {"id":"pics.rar","text":"pics.rar","parent":"Junk"}
        ];

    function shiftDoubles(double) {
      var sorted = mm.filter(function(element) {

        if (element.id != double) {
          return element;
        }

      })
      return sorted;
    }

document.write(JSON.stringify(shiftDoubles("Junk")));

4 Comments

But, for this I have to manually enter the ID's one by one. I want to clean the whole array without having to worry about the ID
Hence the If the ID of the objects is always the same you can do this: If you want that look at the other answer.
Isn't there any function that can do it? I'm assuming that the objects being values wont make any difference
@SaiKrishnaDeep: I can write something similar as that Mykybo did, but that's unnecessary considering the fact that his code does exactly what you need.

Your Answer

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