# Function to create new array

**URL:** <https://forum.kirupa.com/t/function-to-create-new-array/324054>\
**Category:** Uncategorized\
**Created:** [July 30, 2011, 6:00pm UTC](https://forum.kirupa.com/t/function-to-create-new-array/324054 "2011-07-30T18:00:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kill\_robot\_kill](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kill_robot_kill/32/359_2.png) [@kill\_robot\_kill](https://forum.kirupa.com/u/kill_robot_kill)\
**Post date:** [July 30, 2011, 6:00pm UTC](https://forum.kirupa.com/t/function-to-create-new-array/324054/1 "2011-07-30T18:00:15Z")

</div>

Here’s what I’d like to do.  
Call a function that looks at an existing array and creates a new array.  
I don’t want to copy the existing array, but I want to use its length, to populate the new array. I want to be able to name the new array in my function declaration (possible?).  
The prefix parameter is used to push existing variables into the new array.

```auto

function createNewArray(oldArray:Array, newArray:Array, prefix:String):void {
	var arrlength:uint = oldArray.length;
	//
	for (var i:uint = 0; i<arrlength; i++) {
		newArray.push(this[prefix+i]);
	}
}
var answer_0:String = "no no no"; 
var answer_1:String = "blah blah blah"; 
var answer_2:String = "yeah ok"; 
// var answers:Array = new Array();

createNewArray(questions, answers, "answer_");

```

declaring the new Array before calling the function works, I was just wondering if it was possible to skip this step, in order to make the function call/array creation more dynamic.
