โ08-28-2020 06:50 AM
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.
Solved! Go to Solution.
โ08-28-2020 04:28 PM
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)})
โ08-28-2020 10:40 AM
Have you tried the Group by Fields Snap?
โ08-28-2020 04:28 PM
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)})
โ08-29-2020 01:43 AM
@alchemiz Thanks a lot. It worked.
โ08-30-2020 11:36 PM
Glad to be of help ๐