ArrayUnshift

Description

This method adds one or more elements to the beginning of an array and returns the new length of the array.

Returns

The new size of the array.

Syntax

ArrayUnshift(array,object)

Member function

array.unshift(item)

History

ColdFusion (2021 release): Added this function.

Parameters

Parameter

Required/Optional

Description

array

Required

The array where an object is to be added.

object

Required

The object to add at the beginning of the array.

Example

Example 1

<cfscript>
    arr=["Mar","Apr","May","Jun","Jul"]
    unshifted=ArrayUnshift(arr,"Jan")
    WriteOutput(unshifted) // Returns 6
</cfscript>

Example 2

<cfscript>
    arr=["Mar","Apr","May","Jun","Jul"]
    // List as first element
    unshifted=ArrayUnshift(arr,"Jan,Feb") // Returns 6
    WriteOutput(unshifted)
</cfscript>

Example 3

<cfscript>
    arr=["Mar","Apr","May","Jun","Jul"]
    // Array as first element
    unshifted=ArrayUnshift(arr,["Jan,Feb"])
    WriteOutput(unshifted) // Returns 6
</cfscript>

Example 4

<cfscript>
 arr=["Mar","Apr","May","Jun","Jul"]
 // Struct as first element
 elem={"id":101,"name":"John"}
 unshifted=ArrayUnshift(arr,elem)
 WriteOutput(unshifted) // Returns 6
</cfscript>

Get help faster and easier

New user?