You have been redirected from an outdated version of the article. Below is the content available on this topic. To view the old article click here.

slice()

Usage in StructrScript

slice(collection, start, end)
slice(queryFunction, start, end)

Usage in JavaScript

Structr.slice(function, start, end)

Description

This method returns elements from the given collection, list or array, starting at the given start argument, and ends at the given end argument.
If a queryFunction i used (i.e. find() or search) the start and end index are inserted into the cypher statement as LIMIT and SKIP parameters.

Note

  1. The JavaScript implementation of slice() works slightly differently from the StructrScript implementation. The JavaScript version only allows the queryFunction version from StructrScript. For regular JavaScript arrays you can use JavaScripts Array.slice function. Every queryFunction (i.e. find() and search()) inside the function will be sliced analogous to the StructrScript version.

  2. slice(queryFunction, start, end) is intended to be used in conjunction with the batch() function in batched maintenance processes and therefor only available for privileged users. The slice start/end are simply ignored for non-admin users - there is no need to rewrite code.

Examples

StructrScript

${slice(merge("a", "b", "c", "d"), 1, 2)}

Would yield "b"

${slice(find('User'), 0, 10)}
${slice(find('User'), 0, request.count)}

Would return the first 10 users or the first request.count users.

JavaScript

${{
	var retVal = Structr.slice(function() {
		var users = Structr.find('User');
		var files = Structr.find('File');

		return {
			f: files,
			u: users
		}
	}, 0, 2);
	
	Structr.print(retVal.f);
	Structr.print(retVal.u);
}}

Would return an object with the first 2 files and users.

Search results for "slice()"

slice()

IMPORTANT: The slice function has been removed with version 3.5 in favor of the advanced find access pattern.

This method returns elements from the given collection, list or array, starting at the given start argument, and ends at the given end argument.
If a queryFunction i used (i.e. find() or search) the start and end index are inserted into the cypher statement as LIMIT and SKIP parameters.

slice(queryFunction, start, end)