saihkr
saihkr

Reputation: 59

How to Get unique values based on multiple columns from an array in Angular 5?

I have array similar to

[{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }]

In which unique combination of X & Y are required.

Output is :

[{ 's':1,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }]

Since X & Y values are same in 1st & 2nd item. 1 has to be removed.

For UniqBy Single column i am using

_.uniqBy([{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }], 'y');

But i need for the Combination Of 2 or more keys.

_.uniqBy([{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }], '[x]','[y]'); 

and many. None of them worked/

[{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y

_.uniqBy([{ 's':1,'x': 1,'y':1 }, { 's':2,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }], 'y');



[{ 's':1,'x': 1,'y':1 },{ 's':3,'x': 1,'y':2 },{ 's':4,'x': 2,'y':1 },{ 's':5,'x': 2,'y':2 }]

Upvotes: 4

Views: 4882

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44145

Use filter and findIndex for an extremely simple vanilla JavaScript solution (everything done in Lodash can be done in JavaScript, Lodash just simplifies things):

const arr = [{'s':1,'x':1,'y':1},{'s':2,'x':1,'y':1},{'s':3,'x':1,'y':2},{'s':4,'x':2,'y':1},{'s':5,'x':2,'y':2}];

const res = arr.findIndex(({ x, y }, i, a) => i == a.findIndex(e => e.x == x && e.y == y));

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


Lodash solutions:

Using _.uniqBy comparing the two fields as a comma-separated string of values:

const arr = [{'s':1,'x':1,'y':1},{'s':2,'x':1,'y':1},{'s':3,'x':1,'y':2},{'s':4,'x':2,'y':1},{'s':5,'x':2,'y':2}];

const res = _.uniqBy(arr, ({ x, y }) => `${x}${y}`);

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Using _.uniqWith:

const arr = [{'s':1,'x':1,'y':1},{'s':2,'x':1,'y':1},{'s':3,'x':1,'y':2},{'s':4,'x':2,'y':1},{'s':5,'x':2,'y':2}];

const res = _.uniqWith(arr, ({ x, y }, { x: x1, y: y1 }) => x == x1 && y == y1);

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Upvotes: 2

Alan Friedman
Alan Friedman

Reputation: 1610

You can use a callback function and concatenate the numbers.

console.log(_.uniqBy([
   { 's':1,'x': 1,'y':1 },
   { 's':2,'x': 1,'y':1 },
   { 's':3,'x': 1,'y':2 },
   { 's':4,'x': 2,'y':1 },
   { 's':5,'x': 2,'y':2 }
], el => `${el.x}${el.y}`)); 
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Upvotes: 0

Related Questions