sympi
sympi

Reputation: 763

filter objects from array that have the same value at given key

Let's say we have an array that looks like this:

[
    {
        id: 0,
        name: 'A'
    },
    {
        id: 1,
        name:'A'
    },
    {
        id: 2,
        name: 'C'
    },
    {
        id: 3,
        name: 'B'
    },
    {
        id: 4,
        name: 'B'
    }
]

I want to keep only this objects that have the same value at 'name' key. So the output looks like this:

[
    {
        id: 0,
        name: 'A'
    },
    {
        id: 1,
        name:'A'
    },
    {
        id: 3,
        name: 'B'
    },
    {
        id: 4,
        name: 'B'
    }
]

I wanted to use lodash but I don't see any method for this case.

Upvotes: 3

Views: 19691

Answers (4)

jonahe
jonahe

Reputation: 5010

A lodash approach that may (or may not) be easier to follow the steps of:

const originalArray = [{ id: 0, name: 'A' }, { id: 1, name: 'A' }, { id: 2, name: 'C' }, { id: 3, name: 'B' }, { id: 4, name: 'B' }];

const newArray =
      _(originalArray)
      	.groupBy('name') // when names are the same => same group.  this gets us an array of groups (arrays)
        .filter(group => group.length == 2) // keep only the groups with two items in them
        .flatten() // flatten array of arrays down to just one array
        .value();
        
console.log(newArray)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

Upvotes: 5

Rajesh
Rajesh

Reputation: 24955

You can try something like this:

Idea:

  • Loop over the data and create a list of names with their count.
  • Loop over data again and filter out any object that has count < 2

var data = [{ id: 0, name: 'A' }, { id: 1, name: 'A' }, { id: 2, name: 'C' }, { id: 3, name: 'B' }, { id: 4, name: 'B' }];

var countList = data.reduce(function(p, c){
  p[c.name] = (p[c.name] || 0) + 1;
  return p;
}, {});

var result = data.filter(function(obj){
  return countList[obj.name] > 1;
});

console.log(result)

Upvotes: 9

Faly
Faly

Reputation: 13356

A shorter solution with array.filter and array.some:

var data = [ { ... }, ... ];  // Your array
var newData = data.filter((elt, eltIndex) => data.some((sameNameElt, sameNameEltIndex) => sameNameElt.name === elt.name && sameNameEltIndex !== eltIndex));
console.log("new table: ", newTable);

Upvotes: 4

Nina Scholz
Nina Scholz

Reputation: 386868

You could use a hash table and a single loop for mapping the objects or just an empty array, then concat the result with an empty array.

var data = [{ id: 0, name: 'A' }, { id: 1, name: 'A' }, { id: 2, name: 'C' }, { id: 3, name: 'B' }, { id: 4, name: 'B' }],
    hash = Object.create(null),
    result = Array.prototype.concat.apply([], data.map(function (o, i) {
        if (hash[o.name]) {
            hash[o.name].update && hash[o.name].temp.push(hash[o.name].object);
            hash[o.name].update = false;
            return o;
        }
        hash[o.name] = { object: o, temp: [], update: true };
        return hash[o.name].temp;
    }));

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

Upvotes: 2

Related Questions