0

This doesn't work

   const uniqueArray = this.allusers.user_ids.filter((thing,index) => {
              return index === this.allusers.user_ids.findIndex(obj => {
                return JSON.stringify(obj) === JSON.stringify(thing);
              });
            });
    this.allusers.user_ids = uniqueArray

This is the example of my json

0:{
code: "sdsdll"
id: 10
type: "New User"
unit: "Amount"
updated_at: "2019-08-20 09:01:24"
user_ids: (2) [2, 2, 3, 4, 3]
value: "12.00"
__proto__: Object
}
1: {code: "ssddl", id: 9, code: "sklsdsd",...........…}
2: {code: "sdds", id: 11, code: "dsfsdf232",...........…}

I want to remove the duplicates from user_ids array inside array of objects.

rohiteb
  • 49
  • 3
  • @Vaibhav none of the solutions shown there will work for objects. – Jonas Wilms Aug 20 '19 at 09:39
  • Remove duplicated from `const user_ids = [2, 2, 3, 4, 3]` or `const someObj = { user_ids: [2, 2, 3, 4, 3] }`. I don't see a big difference in how the the given array will be filtered. – Vaibhav Vishal Aug 20 '19 at 09:44

4 Answers4

1

Try (something like this)*:

Immutable way:

this.allusers = this.allusers.map(userObj => {
   return Object.assign({}, userObj, {
      user_ids: Array.from(new Set(userObj.user_ids))
   })
});

(*) iterating the object array and mapping each object to a new object with the duplicate user_ids removed by the new Set(...) and in the form of an Array by the Array.from(...)

Mutating original objects: (as @Kaddath mentioned in the comments this keeps other possible existing references to the user objects as it mutates them instead of replacing)

this.allusers.forEach(userObj => {
   userObj.user_ids = Array.from(new Set(userObj.user_ids))
});
Carlos Sá
  • 602
  • 3
  • 10
  • The OP's code indicates the original array is to be modified, IMO using `map` (which is not recommended to modify original array) and a clone with `Object.assign` are useless here. A `forEach` with `Array.from(new Set(` should be enough for the task – Kaddath Aug 20 '19 at 09:35
  • What is *something like this?* – Jonas Wilms Aug 20 '19 at 09:42
  • @Kaddath, if that were true the OP wouldn't be using `filter`. – Andy Aug 20 '19 at 10:10
  • @JonasWilms the code is quite simple I didn't take the time to explain it in detail, hence "Something like this" – Carlos Sá Aug 20 '19 at 10:21
  • @Kaddath I noticed OP is mutating the old array hence the reassignment, to achieve the pretended result by an immutable way or to mutate the array is a matter of choice since the OP did not specify any preference – Carlos Sá Aug 20 '19 at 10:24
  • @Andy there seem to be 2 levels of arrays here, `this.allusers` and `user_ids`, of course the second one is replaced (`.filter`), but remove the `const` and you're left with `this.allusers.user_ids = this.allusers.user_ids.filter(...);` so the first array is kept. – Kaddath Aug 20 '19 at 10:26
  • 1
    @CarlosSá it was just a comment that can be useful not only for the OP but for future readers too. Never said this would not do the job, but in case someone kept references to the users elsewhere and the references are lost after this code, it can be useful to know why – Kaddath Aug 20 '19 at 10:30
  • @Kaddath Yes, I didn't assume other references existing. I've updated my answer to reflect both immutable and non-immutable code. It covers more cases. Thank you for pointing it out! – Carlos Sá Aug 20 '19 at 10:41
1

You can use a set to dedupe an array. All you then need to do is convert it back to an array: [...new Set(user_ids)]

const data = [{"code":"sdsdll","id":10,"type":"New User","unit":"Amount","updated_at":"2019-08-20 09:01:24","user_ids":[2,2,3,4,3],"value":"12.00"},{"code":"sdsdll2","id":101,"type":"New User 2","unit":"Amount","updated_at":"2019-08-20 09:01:24","user_ids":[4,11,2,2,3,4,4,3],"value":"12.00"}];

const out = data.map(({ user_ids, ...rest }) => {
  const dedupedIds = [...new Set(user_ids)];
  return { user_ids: dedupedIds, ...rest };
});

console.log(out);
Andy
  • 61,948
  • 13
  • 68
  • 95
1

Look like your allusers is an array so you can not call .user_ids on it. You can loop through it and process user_ids field of each element with Set.

let allusers = [{code: "sdsdll",id: 10,type: "New User",unit: "Amount",updated_at: "2019-08-20 09:01:24",user_ids: [2, 2, 3, 4, 3],value: "12.00"},{code: "sdsdll2",id: 101,type: "New User 2",unit: "Amount",updated_at: "2019-08-20 09:01:24",user_ids: [4, 2, 2, 3, 4, 4, 5],value: "12.00"}];

allusers.forEach((user) => user.user_ids = [...new Set(user.user_ids)]);

console.log(allusers);
 
Cuong Le Ngoc
  • 11,595
  • 2
  • 17
  • 39
0

If you want to delete the duplicate values, you can do as follows:

var user_ids = Array.from(new Set(user_ids));
freedev111
  • 132
  • 4