CFLib.org – Common Function Library Project

arrayOfStructsFind(array, searchKey, value)

Last updated June 11, 2009

author

Nath Arduini

Version: 0 | Requires: CF5 | Library: DataManipulationLib

Description:
This function searches for an element in an array of structures, using the key name and a value as criteria.

Return Values:
Returns the numeric index of a match.

Example:

<cfset aOfStructs = arrayNew(1)>
<cfset arrayAppend(aOfStructs,structNew())>
<cfset aOfStructs[arrayLen(aOfStructs)].firstName = "Jeff">
<cfset aOfStructs[arrayLen(aOfStructs)].lastName = "Tapper">
<cfset aOfStructs[arrayLen(aOfStructs)].city = "Astoria">
<cfset arrayAppend(aOfStructs,structNew())>
<cfset aOfStructs[arrayLen(aOfStructs)].firstName = "Jon">
<cfset aOfStructs[arrayLen(aOfStructs)].lastName = "Tapper">
<cfset aOfStructs[arrayLen(aOfStructs)].city = "Brookline">
<cfset arrayAppend(aOfStructs,structNew())>
<cfset aOfStructs[arrayLen(aOfStructs)].firstName = "Dan">
<cfset aOfStructs[arrayLen(aOfStructs)].lastName = "Tapper">
<cfset aOfStructs[arrayLen(aOfStructs)].city = "Suffield">

<cfoutput>
#ArrayOfStructsFind(aOfStructs,"lastName","Tapper")#<BR>
#ArrayOfStructsFind(aOfStructs,"city","Suffield")#
</cfoutput>

Parameters:

Name Description Required
array Array to search. Yes
searchKey Key to check in the structs. Yes
value Value to search for. Yes

Full UDF Source:

/**
 * Returns the position of an element in an array of structures.
 * 
 * @param array 	 Array to search. (Required)
 * @param searchKey 	 Key to check in the structs. (Required)
 * @param value 	 Value to search for. (Required)
 * @return Returns the numeric index of a match. 
 * @author Nath Arduini (nathbot@gmail.com) 
 * @version 0, June 11, 2009 
 */
function arrayOfStructsFind(Array, SearchKey, Value){
	var result = 0;
	var i = 1;
	var key = "";
	for (i=1;i lte arrayLen(array);i=i+1){
		for (key in array[i])
		{
			if(array[i][key]==Value and key == SearchKey){
				result = i;
				return result;
			}
		}
	}
    
    return result;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson