How can I push duplicate values in to an array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2022 08:50 PM
HI,
I'm working on GlideExcelParser API, How to Find the Duplicates in the Excel for specific columns like Asset tag and Serial Number ???
My Requirement is to Store all the Assets tag and Row Number into an Array and to push only the Duplicates into Separate Array.
Can someone help me ??
Thanks,
M
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2022 09:07 PM
To remove duplicates in the array, You can use OOB ArrayUtil Script Include
var noDuplicates = new ArrayUtil().unique(arr);//arr contains the array values
To add Duplicates into the new array, try the below
var duplicates = []
for (var i = 0; i < yourOrginalArray.length; i++) {
if (yourOrginalArray[i + 1] === yourOrginalArray[i]) {
duplicates.push(yourOrginalArray[i]) //Push duplicates into new array
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 12:40 AM
HI Sai,
Tried below script, it is not working .
var obj = [ // left side : Row Number // Right side : Asset tag
{ "1": "assetag100" },
{ "2": "assetag101" },
{ "3": "assetag100" },
{ "4": "assetag1001" },
{ "5": "assetag101" },
];
var arr = new Array();
arr.push(obj);
var v_aAssetTagSorted = arr.sort();
gs.print(JSON.stringify(v_aAssetTagSorted));
var duplicates = [];
for (var i = 0; i < v_aAssetTagSorted.length; i++) {
if (v_aAssetTagSorted[i + 1] === v_aAssetTagSorted[i].assettag) {
duplicates.push(v_aAssetTagSorted[i].assettag) //Push duplicates into new array
}
}
gs.print(JSON.stringify(duplicates));