1

i'm having trouble to remove duplicated object from my array

example:

var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}];

in this example i have 3 objects, and i want to remove the object that have the duplicated place

5
  • 4
    FYI that's not an array - it's a syntax error. You need to replace the brackets with [] Commented Jul 13, 2015 at 16:13
  • 1
    First you have to decide, which one of them you want to keep. OR you want to remove both? Commented Jul 13, 2015 at 16:14
  • i want to remove just one, dont matter wich one Commented Jul 13, 2015 at 16:16
  • @MarcosViniciusdeSouzaGouve stackoverflow.com/questions/2218999/… Commented Jul 13, 2015 at 16:22
  • You can get an idea how to remove it from here: stackoverflow.com/questions/26832580/… Commented May 22, 2017 at 16:04

6 Answers 6

1

Just in case someone wonders: underscore.js solution:

var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}];

_.uniq(list, function(item, key, a) { 
    return item.place;
})

Example Fiddle

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

Comments

1

A simple one:

var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}];

list.forEach(function(i) {
  var duplicates = list.filter(function(j) {
    return j !== i && j.place == i.place;
  });
  duplicates.forEach(function(d) { list.splice(list.indexOf(d), 1); });
});

// list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}];
document.write(JSON.stringify(list));

Comments

0

As you added:

i want to remove just one, dont matter wich one

If you want to remove duplicated items and keep only the first occcurence of particular place, you can simply use a simple loop to re-create a new array from the input:

var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}];

var uniqPlace = function(array){
    var result = [];
    array.forEach(function(el){
        if (result.filter(function(n){ return n.place === el.place }).length==0){
            result.push(el);
        }
    })
    return result;
}

Output:

uniqPlace(list);

[{"place":"AAA","name":"Me"},{"place":"BBB","name":"You"}]

Comments

0

Try this.

var result = {};
for (i = 0, n = arr.length; i < n; i++) {
    var item = arr[i];
    result[ item.place + " - " + item.name ] = item;
}

Loop the result again, and recreate the array.

i = 0;    
for(var item in result) {
    clearnArr[i++] = result[item];
}

Comments

0

Create a object to store the items by their place value, as the new item with the same key will overwrite the old one, this will easily remove all dulplicates.

var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}];
var removeDuplicate = function(list) {
  var keyStore = {};
  var output = [];
  // If you want to creata totally new one from old, use 
  // list = JSON.parse(JSON.stringify(list));
  // The above commented out code will create a copy of list, so the items in output will not affect the original ones.
  list.forEach(function(item) {
    // new one overwrites old one.
    keyStore[item.place] = item;
  });
  
  var key;
  for (key in keyStore) {
    output.push(keyStore[key]);
  }
  
  return output;
};

console.log(removeDuplicate(list));

Comments

0

3 way to remove duplicate objects from array

let list = [{place:"AAA",name:"Me"}, 
            {place:"BBB",name:"You"}, 
           {place:"AAA",name:"Him"}];



let output1 = Array.from(new Set(list.map(list=>list.place))).map(place=>{
  return {
   place: place,
   name: list.find(a=>a.place===place).name
  }
})
console.log('------------------------1st way')
console.log(output1)

let output2 = list.reduce((accumulator, element) => {
     if (!accumulator.find(el => el['place'] === element['place'])) {
          accumulator.push(element);
      }
     return accumulator;
   },[]);
console.log('------------------------2nd way')
console.log(output2)


const output3 = [];
const map = new Map();
for (const object of list) {
    if(!map.has(object.place)){
        map.set(object.place, true);
        output3.push({
            place: object.place,
            name: object.name
        });
    }
}
console.log('------------------------3rd way')
console.log(output3)

Comments

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.