0

I have an array like

let x1=[{"data":"hello","id":2},{"data":"hellr","id":17},{"data":"hellr","id":24}]

It is a sample array.The array is huge what i want is to convert it into set on property data.Is there a way to do this so that I can remove duplicate of data?

Always Helping
  • 14,316
  • 4
  • 13
  • 29

3 Answers3

0

You can pass the result from Array#map to the Set constructor to find all the unique data values.

let x1=[{"data":"hello","id":2},{"data":"hellr","id":17},{"data":"hellr","id":24}];
const set = new Set(x1.map(({data})=>data));
console.log([...set]);

If you need the objects themselves, you can use Array#filter while adding to a Set.

let x1=[{"data":"hello","id":2},{"data":"hellr","id":17},{"data":"hellr","id":24}];
const set = new Set;
const res = x1.filter(({data})=>!set.has(data) && set.add(data));
console.log(res);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

Well, this is fast and draft way

let x2 = new Set(
  x1.map(({ data }) => {
    return data
  })
)
Damian Grzanka
  • 275
  • 2
  • 13
  • Instead of just simply stating that it's a "fast and draft way", an additional *explanation* on the code you've provided will be more *beneficial* to others who view your code. – Edric Aug 11 '20 at 03:17
0

Sorry if I'm not understanding your question correctly, but if you want to remove duplicate objects with the same data values in your array, you can do this

let x1 = [{"data":"hello","id":2},{"data":"hellr","id":17},{"data":"hellr","id":24}];

x1 = x1.filter((item, index, self) => self.findIndex(i => i.data === item.data) === index);
Alfred
  • 644
  • 4
  • 12
  • let sortByProperty = function (property) { return function (x, y) { return ((x[property] === y[property]) ? 0 : ((x[property] > y[property]) ? 1 : -1)); }; } finalArray=(finalArray.sort(sortByProperty('label'))) // Now I am getting Sorting of the form Data in alphabetical order "A followed by B followed by C so on and then a b c so on but what i want is A followed by a B followed by C followed by c and so on. Could you help me in that? – DEEPAK DOGRA Aug 11 '20 at 06:48
  • I believe this is a different question and you should create a new post for this, but if you want to sort an array of objects by a property value, you can do `x1.sort((a,b) => (a.data > b.data) ? 1 : ((b.data > a.data) ? -1 : 0));` – Alfred Aug 11 '20 at 08:00
  • [ { data: 'Ellr', id: 17 }, { data: 'Hello', id: 2 }, { data: 'ellr', id: 24 }, { data: 'hello', id: 2 } ] Using Your function i achieved this but what I want is [ { data: 'Ellr', id: 17 }, { data: 'ellr', id: 2 }, { data: 'Hello', id: 24 }, { data: 'hello', id: 2 } ] – DEEPAK DOGRA Aug 11 '20 at 11:54
  • If you want to perform case insensitive comparison, simply convert them into lowercase with `toLowerCase()` like this `x1.sort((a,b) => (a.data.toLowerCase() > b.data.toLowerCase()) ? 1 : ((b.data.toLowerCase() > a.data.toLowerCase()) ? -1 : 0));` – Alfred Aug 12 '20 at 01:24
  • But Requirement is to maintain exact case in my final Result – DEEPAK DOGRA Aug 12 '20 at 09:57
  • The usage of `toLowerCase()` in the function above is just for sorting purpose, the final output retains the case. Do give it a try. – Alfred Aug 12 '20 at 17:27