The following functions can be used to manipulate and retrieve details from objects and arrays. These should be written in the following formats: . ''array.function(CreateFunction("input","handling"))'', example - ''myArray.forEach(CreateFunction("m,i,a","m='lkd' && array2.push(m)"))'' if you had an array and wished to push all elements matching 'lkd' to another array called "array2" this would allow you to do so. Confused? Same, We'll do some examples soon. Click each function name in the table below for more details of it, with examples. == Functions == ||'''Function ''' ||<#cccccc>'''Description ''' |||| || || ||[[#some|.some()]] ||Returns true if "some" of the values are true, else returns false. || ||[[#filter|.filter()]] ||Returns the array with values filtered out that match the filter input. || ||[[#forEach|.forEach()]] ||Returns a value for each separate element in an array after executing the provided function once per array element. || ||[[#map|.map()]] ||Returns a new array with the results of calling a provided function on every element within this array. || ||[[#every|.every()]] ||Returns true if all elements in the array passed the input parameters (all where true). || == CreateFunction == Understanding CreateFunction() might seem hard at first, but once you play with some examples it can be easy to understand. CreateFunction allows the user to define and create there own functions which can be used on the Functions seen above. The CreateFunction() requires an arguments string, and a formula string. These are formatted like so: CreateFunction("Arguments","Formula") The arguments is what to pass into the function, when using functions like forEach and map, you may be required to pass in an "array" and an "index" argument, this is for the internal processing used in the forEach and map functions. All the functions do however require one argument. This argument is referred too as the "currentValue". This is because this indicates the current value we're handling from the array the function is being used on. Imagine we have an array of 4 numbers: a=[1,2,3,4] to use the functions above on this array it will need to process each number (1,2,3 and 4). While doing so, this number is referred too as the currentValue. The example below shows how the currentValue is set. . !MyFunction = CreateFunction("currentValue,Index,Array","Arr2.push(currentValue)") As you can see in this example, the currentValue is an argument, it is also used in the formula to complete a process. Expanding on this function, we could use forEach on an array Arr=[1,2,3,4] to add the elements the existing array to a new array called Arr2. . Arr = [1,2,3,4] //Existing array. . Arr2 = [] //New empty array. . !MyFunction = CreateFunction("currentValue,Index,Array","Arr2.push(currentValue)") //Function to use in .forEach() function. . Arr.forEach(!MyFunction) //using the !MyFunction CreateFunction on the array 'Arr'. . echo "the old array is {Arr}, the new array after processing is: {Arr2}" //Echoing the output. The output of this would be: . the old array is 1,2,3,4, the new array after processing is: 1,2,3,4 In the formula section of this CreateFunction we've put: "Arr2.push(currentValue)". This is a simple command telling it to [[Arrays#push|push]] the currentValue to the array Arr2. This now means we have two totally identical arrays that are totally separate from one another. = some = '''Usage:''' .some(CreateFunction("input","handling")) the .some() function requires at least 1 input. This input is how you'll reference the current value (elements of the array you're running this function against). .some() is a boolean check to see if at least one or more elements in an array return true. If all are false this will return false, but if at least one is true, the result will be true. '''Example:''' . lessthan10 = CreateFunction("x,ind,arr", "x < 10") //Create a function to check if 'x' (current value) is '<' (less than) 10. 'ind,arr' allows us to create an index and an array. this is just for internal function handling. . arr = [ 20, 5, 10, 11, 7, 3.4, 21, 1, 7, 9 ] //Create an array of numbers. . if arr.some(lessthan10) echo "At least one value is less than 10" //If at least one of the "arr" elements is 'lessthan10', echo "At least one value is less than 10". '''Result:''' . At least one value is less than 10 = filter = '''Usage:''' .filter(CreateFunction("input","handling")) the .filter() function requires at least 1 input. This input is how you'll reference the current value (elements of the array you're running this function against). .filter() returns an array with the values from the input array. But only if these values passed the filter function entered. If one does not pass, that one will not exist in the new array. '''Example:''' . lessthan10 = CreateFunction("x,ind,arr", "x < 10") //Create a function to check if 'x' (current value) is '<' (less than) 10. 'ind,arr' allows us to create an index and an array. this is just for internal function handling. . arr = [ 20, 5, 10, 11, 7, 3.4, 21, 1, 7, 9 ] //Create an array of numbers. . result = arr.filter(lessthan10) . echo result '''Result:''' . 5, 7, 3.4, 1, 7, 9 = forEach = '''Usage:''' .forEach(CreateFunction("input,Index,Array","handling")) the .forEach() function requires at least 3 inputs. This consists of an Array, and Index and the input current value. The index and array are needed for the function handling. .forEach() returns an array with the values from the input array. But only if these values passed the filter function entered. If one does not pass, that one will not exist in the new array. .forEach Returns a value for each separate element in the array that you've passed in after executing the provided function (!CreateFunction) once per array element. '''Example:''' . a = [ ] //Creates an empty array that we can use to push values to. . pushname = CreateFunction("obj,ind,arr", "a.push(obj.name)") //Creates a function that will push the obj.name to the "a" array. . city.heroes.toArray().forEach(pushname) //.toArray is used to convert the city.heroes object to an array. Each value of this array is the passed though the 'pushname' function. . echo "Heroes are: " + a //Echo the "a" array. '''Result:''' . Heroes are: LKD70,BROX,LKD,SPOCK,SPAM000,SPAM001,SPAM002,SPAM003,SPAM004,SPAM005 = map = '''Usage:''' .map(CreateFunction("input,Index,Array","handling")) the .map() function requires at least 3 inputs. This consists of an Array, and Index and the input current value. The index and array are needed for the function handling. The .map() function will create a new array consisting of the results of running the CreateFunction on each element within the previous array. Every value in the previous array will run the CreateFunction. There output is then appended to the new array at the same index it was at in the original array. '''Example:''' . addTotal = CreateFunction("x,ind,arr", "total+=x") //This function allows us to add all inputted values to the "total" variable (when using it with .forEach() ). . getArchers = CreateFunction("city,ind,arr", "city.cityManager.troop.archer") //This grabs the archer count from a city. . . total = 0 . cities.map(getArchers).forEach(addTotal) //This runs the "getArchers" function in all "cities". This means the output will consist of the archer count for all cities in the account. It then uses forEach on this new array to run the "addTotal" function on each archer count. This will add each of the archer counts to the "total" variable. . . echo "Total (idle) archers in all cities is: {total}." //this simply echo's the 'total' variable. '''Result:''' . Total (idle) archers in all cities is: 320963530. = every = '''Usage:''' .map(CreateFunction("input,Index,Array","handling")) the .every() function requires at least 1 input. This is the current value within the array. the .every() function will return true or false depending on the output of every array element after processing through the function. If just one of the elements returns false, this will return false, but if all returned true, then this will be true. '''Example:''' . limit = !GetResources("f:990b,w:100m,i:10m:10m,s:10m,g:100m") //Sets the variable "limit" to a resources object. . hasResource = CreateFunction("city,index,arr", "city.cityManager.hasResource(limit)") //hasResource function to check if the city hasResource() "limit". . if !cities.every(hasResource) echo "We are low on resource in at least one city!" //Perform "every" on the cities array to check if "every" city returns true on hasResources (This would mean all cities have the specified resources.) if it does not it will echo "We are low on resource in at least one city!". '''Result:''' . We are low on resource in at least one city! ---- CategoryFunctions