JavaScript Remove duplicate objects from array by a certain merge algorithm remove duplicate objects from array javascript,remove duplicate objects from

I think the best way would be to hash each object if it does not already exist, and delete the ones that you found already hashed in your structure. This way, you'd be checking the existence of each object only 1 (depends on your hash scheme).

Answer:1

Just a function if you would like to use.

function merge(arr) {    
   for(var o = {}, i; i=arr.shift(); o[i.name] = i.count + (o[i.name] || 0));
   for(i in o) arr.push({name:i, count:o[i]});
}

Calling :

var myArray = [{"name":"Kirk","count":1},
               {"name":"Spock","count":1},
               {"name":"Kirk","count":1}];

merge(myArray);   

// myArray is now :  [{"name":"Kirk","count":2}, {"name":"Spock","count":1}]
Answer:2

You can use reduce which is actually a fold.

a.reduce(function(p, c) {
        var n = c.name;
        if (p[n])
            p[n].count++;
        else
            p[n] = c;
        return p;
    }, {})

will give you a object with "Kirk" and "Spock" as the key, what you want as values.

Answer:3

I know this is an old question, but I couldn't resist trying to solve it. Instead of two maps and a filter, we use a sort and then a reduce. This was a fun one to sort out :-)

function mergeDuplicates(list, prop, cb){
  return list.sort(function(a,b){
    if(a[prop] < b[prop]){ return -1;}
    if(a[prop] > b[prop]){return 1;}
    return 0;
  }).reduce(function(acc, item, index, array){
    if(index > 0 && array[index-1][prop] === item[prop]){
      cb(acc[acc.length-1], item);
      return acc;
    }else{
      var newItem = Object.assign({}, item);
      cb(newItem);
      acc.push(newItem);
      return acc;
    }
  }, []);
}

Then use it like this:

var newList = mergeDuplicates(list, "name", function(item, dup){
    if(dup){
      item.count++;
    }else{
      item.count = 1;
    }        
});

EDIT: Here's another take at it using reduce and using an object as a hashmap to store duplicates (similar to some of the other answers). This one uses ramdajs

const mergeDups = (cb, prop, list) => R.pipe(
  R.reduce((acc, item) => (
    R.has(item[prop], acc) ?
      R.assoc(item[prop], cb(acc[item[prop]], item), acc) :
      R.assoc(item[prop], cb(item), acc)     
  ), {}), 
  R.values
)(list);

const cb = (i, d) => ( !R.isNil(d) ? 
  R.assoc('count', i.count + 1, i) :
  R.assoc('count', 1, i) )

mergeDups(cb, 'name', items);

Here it is in the repl on Ramda's site

Answer:4

Try this better i'll useful resolve your issues

cleanup(arrayOfObj, 'name');

function cleanup(arr, prop) {
  var new_arr = [];
  var lookup = {};
  for (var i in arr) {
   lookup[arr[i][prop]] = arr[i];
  }
  for (i in lookup) {
   new_arr.push(lookup[i]); 
  }
  return new_arr;
}
Answer:5

Take a look at the code below: var pocket = { cash: 1000, showCash: function() { return this.cash; } }; var thomas = { name: "Thomas", work: ...

Take a look at the code below: var pocket = { cash: 1000, showCash: function() { return this.cash; } }; var thomas = { name: "Thomas", work: ...

I'm trying to write a pretty simple app: I have some games (say chess, tictactoe... whatever), and there are several boards of each game. I want to show a list of games, and then show a list of boards ...

I'm trying to write a pretty simple app: I have some games (say chess, tictactoe... whatever), and there are several boards of each game. I want to show a list of games, and then show a list of boards ...

function f(message) { console.log(message,arguments); } f("H", "e", "l", "l", "o"); It shows: H + ["H", "e", "l", "l", "o"] Question: why message only shows H, not ["H", "e", "l", "l", "o"]?

function f(message) { console.log(message,arguments); } f("H", "e", "l", "l", "o"); It shows: H + ["H", "e", "l", "l", "o"] Question: why message only shows H, not ["H", "e", "l", "l", "o"]?

Suppose I want to call some JavaScript function. I should keep the required resource file (the JavaScript file) in the application folder and link. But what are the built-in functions like eval(), ...

Suppose I want to call some JavaScript function. I should keep the required resource file (the JavaScript file) in the application folder and link. But what are the built-in functions like eval(), ...

  1. javascript function loaded
  2. javascript function not loaded
  3. javascript function page loaded
  4. javascript check if function loaded
  5. javascript document loaded function
  6. javascript function after page loaded
  7. javascript function after dom loaded
  8. javascript run function when loaded
  9. javascript wait function loaded