Copy link to clipboard
Copied
Hi all.
How can make array of unique elements ?
Heed to get from array -> array1
<cfset array = arrayNew(1) />
<cfset array1 = arrayNew(1) />
<cfset array[ 1 ] = { id = '1' } />
<cfset array[ 2 ] = { id = '1' } />
<cfset array[ 3 ] = { id = '1' } />
<cfset array[ 4 ] = { id = '2' } />
<cfset array[ 5 ] = { id = '3' } />
<cfset array[ 6 ] = { id = '4' } />
<cfset array[ 7 ] = { id = '4' } />
<cfset array[ 8 ] = { id = '5' } />
<cfdump var="#array#" label="Current" />
<cfset array1[ 1 ] = { id = '1' } />
<cfset array1[ 2 ] = { id = '2' } />
<cfset array1[ 3 ] = { id = '3' } />
<cfset array1[ 4 ] = { id = '4' } />
<cfset array1[ 5 ] = { id = '5' } />
<br /><br />
<cfdump var="#array1#" label="Needfull" />
Copy link to clipboard
Copied
You would need to search the array each time you appended a structure. Then append the structure, only if did not already exist in the array. But that raises the question, is an array really the right type of object to be using in the case? A structure, which does not allow duplicates, would seem more appropriate.
Copy link to clipboard
Copied
if i wont delete elements if they first element eq second element etc.
<cfset limit = arrayLen(array) />
<cfloop from="#limit#" to="1" index="i" step="-1" >
<cfloop from="1" to="#i-1#" index="j" step="1">
<cfif array
<cfset ArrayDeleteAt(array.id, j) />
</cfif>
</cfloop>
</cfloop>
<cfdump var="#array#" label="Needfull" />
but i get this error:
help with syntax pls
Copy link to clipboard
Copied
No. The easier way is to search the array _before_ elements are added, not after. If for whatever reason you cannot do that, it would be easier to use a structure to eliminate the duplicates. Then convert it back to an array.
<cfset keep = {} >
<cfloop array="#array#" index="elem">
<cfset keep[elem.id] = elem>
</cfloop>
<cfset finalArray = []>
<cfloop collection="#keep#" item="key">
<cfset arrayAppend(finalArray, keep[key]) >
</cfloop>
<cfdump var="#finalArray#" label="finalArray" />
But, why are you using an array here?
Copy link to clipboard
Copied
thx, for your reply, it works correctly, but i incorrect made a condition:
<cfset array = arrayNew(1) />
<cfset array1 = arrayNew(1) /><cfset array[ 1 ] = { name = 'alex', priority = '1' } />
<cfset array[ 2 ] = { name = 'alex', priority = '2' } />
<cfset array[ 3 ] = { name = 'bob', priority = '2' } />
<cfset array[ 4 ] = { name = 'alex', priority = '3' } />
<cfset array[ 5 ] = { name = 'sam', priority = '3' } />
<cfset array[ 6 ] = { name = 'bill', priority = '3' } />
<cfset array[ 7 ] = { name = 'sam', priority = '4' } />
<cfset array[ 8 ] = { name = 'siu', priority = '4' } /><cfdump var="#array#" label="current">
<hr />
<cfset array1[ 1 ] = { name = 'alex', priority = '1' } />
<cfset array1[ 2 ] = { name = 'bob', priority = '2' } />
<cfset array1[ 3 ] = { name = 'sam', priority = '3' } />
<cfset array1[ 4 ] = { name = 'bill', priority = '3' } />
<cfset array1[ 5 ] = { name = 'siu', priority = '4' } /><cfdump var="#array1#" label="expected">
Copy link to clipboard
Copied
It is obvious the desired results are different, but not the reason why. We cannot read your mind You need to provide more information about:
a) what applications conditions determine "unique"
b) what code you have tried and how that code is working differently than you expect
Also, you never answered the question of why you are using an array here or why you cannot perform these checks before each element is added.
Copy link to clipboard
Copied
There are a few 'tricks' that you can use here ...
"Sorting" is what I call an "unexpectedly fast and efficient" operation, because it's one of the most heavily-studied operations in all of data processing ... and has been since the days of Herman Hollerith.