CancelImage Upload

How to load a specific set (slice) of elements out of an array in JavaScript

If you want to load a specific section out of an array in JavaScript, here is the function that you are looking for. Its name is slice, and it does just that, returning a specified slice of an array. The key of course is its definition:

var slice = yourArray.slice(start, end);

The array element by the number of end will not be included in the slice returned. Here an extended example, with the resulting being loaded into a span via getElementById():

<html>
<head>
<title>How to load a specific set (slice) of elements out of an array in JavaScript</title>
<script type="text/javascript">

function arraySlice()
{

         var fruits = new Array('Oranges', 'Bananas', 'Strawberries', 'Apples');

         var slice = fruits.slice(1, 3);

         document.getElementById("display").innerHTML = slice;

}

</script>
</head>
<body onload="javascript:arraySlice();">

<span id="display"></span>

</body>
</html>

For the PHP equivelenant of the JavaScript function slice(), read the following tutorial: How to slice and merge arrays in PHP

Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com