Racking my brain with an array from JSON that has ...
# cfml-general
c
Racking my brain with an array from JSON that has some duplicate rows, I have tried deduping based on copying the row into a new array using a list of key values but somehow I still get 4 dupes at the end. Here is a small sample of the json data it has dupes in it.
c
I'm on my phone but I think this bit might be a problem because the position in each array wouldn't be the same if you are deduping <cfset aralis[x] = arrData[x]> Maybe something more like Arrayappend(aralis, arrdata[row])
👍🏼 1
j
If I run your code, on CF2021, I get a result array with 7 entries and no duplicates. What platform are you on?
c
2021
full json
try that json
that is the one that has dupes
document_id: 105050 has 4 dupes
j
I get an array with 25 entries, out of 115
c
rows 20 - 25
should be 20 out of 115
the logic should be de duping it
I have used this logic hundreds of times to de-dupe. Cant for the life of me understand why its not
@cwoolley I am deduping by not copying current row into the new array (or so it should be doing)
j
I see the dups now but not sure why
c
@JohnT at least I know its not just me 😛
This seems to work
<cfset aralis = ArrayNew(1)>
<!--- loop through array --->
<cfloop from="1" to="#arrayLen(arrData)#" index="row">
<cfset document = arrData[row].documents[1]>
<cfif NOT ListFindNoCase(keyList, document.document_object_id)>
<!--- <cfdump var="#document#"><br> --->
<!--- create a new array and inject a new field --->
<cfset Arrayappend(aralis, arrdata[row])>
<!--- <cfset aralis[x] = arrData[x]> --->
<cfset aralis[x].sendEmail = "false">
<cfset keyList = ListAppend(keyList, document.document_object_id)>
<cfset x=x+1>
</cfif>
</cfloop>
<cfset Arrayappend(aralis, arrdata[row])>
j
Your revised code is better but the problem with the original is at line 35 <cfset aralis[x] = arrData[x]>.
x referred to the last array element stored so would add that to the array and not the new entry at "row'
r
with the provided json. Are you supposed to have 26 rows after removing dups?
I was trying to be fancy and came up with this.
Copy code
var data = json.map((row) => {
	row.hash = hash(row.toString());
	return row;
}).filter((row,index, arr) => {
	var indexes = arr.findAll( (iRow)=>{
		return row.hash eq iRow.hash;
	});
	return index eq indexes.first();       		
});
a
c
Thanks guys it was a big help!!!