Home > OS >  Remove duplicated objects according to one of their key
Remove duplicated objects according to one of their key

Time:07-30

I have an array which has only objects in it and I would like to remove duplicated objects based one of their key value which is val.js.url but wanna make it remaining object the latest one among the duplicated objects so I basically used reverse() in my code`.

My array:

[
  {
    "id": "js_1oc3uiteki22",
    "tab": {
      "title": "title 1",
      "url": "http://google.com"
    },
    "js": {
      "initiator": "https://google.com",
      "url": "http://url.com"
    }
  },
  {
    "id": "js_1oc3uiteki22",
    "tab": {
      "title": "title 2",
      "url": "http://google2.com"
    },
    "js": {
      "initiator": "https://google2.com",
      "url": "http://url.com"
    }
  }
]

Expected result:

[
  {
    "id": "js_1oc3uiteki22",
    "tab": {
      "title": "title 2",
      "url": "http://google2.com"
    },
    "js": {
      "initiator": "https://google2.com",
      "url": "http://url2.com"
    }
  }
]

Actually I have this following code which solves this problem but when I need to use that code 2 times in same js file I guess it's kinda conflicts and doesn't clean the duplicates well in second array.

function dupfix(arr) {
  return arr.reverse().filter(val => {
    if (!this.hasOwnProperty(val.js.url)) {
      return this[val.js.url] = true
    }
    return false
  }, {}).reverse()
}
file = dupfix(file)

So, how can I fix that code above or is there any other better methods to do that? Thanks.

CodePudding user response:

I found that one really fast among other alternatives, https://stackoverflow.com/a/61027136

function removeDuplicates(array, key) {
   let lookup = {};
   array.forEach(element => {
     lookup[element[key]] = element
   });
   return Object.keys(lookup).map(key => lookup[key]);
};

removeDuplicates(array,'objectKey');

CodePudding user response:

You can use like this to filter the duplicates.

const myArr=[
  {
    "id": "js_1oc3uiteki22",
    "tab": {
      "title": "title 1",
      "url": "http://google.com"
    },
    "js": {
      "initiator": "https://google.com",
      "url": "http://url.com"
    }
  },
  {
    "id": "js_1oc3uiteki22",
    "tab": {
      "title": "title 2",
      "url": "http://google2.com"
    },
    "js": {
      "initiator": "https://google2.com",
      "url": "http://url2.com"
    }
  },
  {
    "id": "js_1oc3uiteki23",
    "tab": {
      "title": "title 3",
      "url": "http://google3.com"
    },
    "js": {
      "initiator": "https://google3.com",
      "url": "http://url3.com"
    }
  },
  {
    "id": "js_1oc3uiteki23",
    "tab": {
      "title": "title 4",
      "url": "http://google4.com"
    },
    "js": {
      "initiator": "https://google4.com",
      "url": "http://url4.com"
    }
  }
]


function removeDuplicates(arr) {
  let uniqueIds = [];
  var result = arr.slice().reverse().filter(item => {
       var isDuplicate = uniqueIds.includes(item.id);
          if (!isDuplicate) {
            uniqueIds.push(item.id);
            return item;
          }
       });
       
   return result;
}

console.log(removeDuplicates(myArr));

CodePudding user response:

You can use hash grouping to uniquely identify objects:

const val = [{"id":"js_1oc3uiteki22","tab":{"title":"title 1","url":"http://google.com"},"js":{"initiator":"https://google.com","url":"http://url.com"}},{"id":"js_1oc3uiteki22","tab":{"title":"title 2","url":"http://google2.com"},"js":{"initiator":"https://google2.com","url":"http://url.com"}}];

const uniqs = val.reduce((r, item) => (r[item.js.url] = item, r), {});
const result = Object.values(uniqs);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }

  • Related