cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Remove duplicate values from the JSON array

Tanmay_Sarkar
New Contributor III

Hello All,

Hi, I have a JSON array and I have to remove the duplicates based on a field and then I want the non duplicate elements in one array and the duplicate values in an another array.

Input JSON:
[
{โ€œpNameโ€: โ€œabcโ€,โ€œiNumberโ€: 123 },
{โ€œpNameโ€: โ€œdefโ€,โ€œiNumberโ€: 123 },
{โ€œpNameโ€: โ€œxyzโ€,โ€œiNumberโ€: 890 },
{โ€œpNameโ€: โ€œjklโ€,โ€œiNumberโ€: 456 }
]

Required Output:
[
{โ€œpNameโ€: โ€œabcโ€,โ€œiNumberโ€: 123 },
{โ€œpNameโ€: โ€œdefโ€,โ€œiNumberโ€: 123 }
]
[
{โ€œpNameโ€: โ€œxyzโ€,โ€œiNumberโ€: 890 },
{โ€œpNameโ€: โ€œjklโ€,โ€œiNumberโ€: 456 }
]
Two separate JSON arrays one with non duplicate iNumber and another with the one which were duplicates.

I tried filter((item, pos, a) => a.findIndex(elem => item.iNumber == elem.iNumber) == pos), it didnโ€™t give out the required result it still keeps one of the duplicate values.

1 ACCEPTED SOLUTION

alchemiz
Contributor III

Try using the filter method

e.g.

{}.merge({โ€œUniqueโ€: $array.filter((a,b,c)=> c.filter((x,y,z)=> a[โ€˜iNumberโ€™] == x[โ€˜iNumberโ€™]).length == 1)}, {โ€œDupsโ€: $array.filter((a,b,c)=> c.filter((x,y,z)=> a[โ€˜iNumberโ€™] == x[โ€˜iNumberโ€™]).length != 1)})

image

View solution in original post

4 REPLIES 4

dmiller
Admin Admin
Admin

Have you tried the Group by Fields Snap?


Diane Miller
Community Manager

alchemiz
Contributor III

Try using the filter method

e.g.

{}.merge({โ€œUniqueโ€: $array.filter((a,b,c)=> c.filter((x,y,z)=> a[โ€˜iNumberโ€™] == x[โ€˜iNumberโ€™]).length == 1)}, {โ€œDupsโ€: $array.filter((a,b,c)=> c.filter((x,y,z)=> a[โ€˜iNumberโ€™] == x[โ€˜iNumberโ€™]).length != 1)})

image

@alchemiz Thanks a lot. It worked.

Glad to be of help ๐Ÿ˜€