JS Array slice method

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
stib
Posts: 19
Joined: December 10th, 2006, 10:23 pm
Contact:

August 27th, 2015, 9:18 pm

I was going nuts trying to work out why a script wasn't working. I was using the Javascript array object's slice method:

Code: Select all

selectedLayers = app.project.activeItem.selectedLayers;
layerSlice=selectedLayers.slice(a, b);
But it was returning undefined. Am I doing something wrong or does array.slice() not work in AE?

I ended up building my own replacement:

Code: Select all

function mySlice(inputArray, startIndex, sliceLength){
//this returns two arrays: first a slice of the original containing
//sliceLength elements from the input starting with startIndex
//and a second containing the rest of the original array
	outPutArray=[];
	sliceArray = [];
	for(var i=0; i < inputArray.length; i++){
		if((i < startIndex) | (i >= startIndex + sliceLength)){
			outPutArray.push(inputArray[i]);
		} else {
			sliceArray.push(inputArray[i]);
		}
	}
	return [sliceArray, outPutArray];
}
Post Reply