Extract Elements From Array in JavaScript

To extract element or item from array in JavaScript; Through this tutorial, i am going to show you how to how to extract elements or items from array in javascript with the help of array slice() method.

Extract Elements From Array in JavaScript

Using javaScript slice() method; you can easily extracts a part of an array from a given array and returns a new array.

The following syntax represents the slice() method:

slice(begin, end);

Note that, the js array slice() method takes two optional parameters.

Here,

  • The begin is a position where to start the extraction.
  • The end  is a position where to end the extraction.

Note that, about js array slice() method:

  • If begin is undefined, slice begins from the index 0.
  • If end is omitted, slice extracts through the end of the sequence (arr.length).

Example 2 – Extract portion of an array from array javascript

The following example shows how to copy the portion of an array without changing the original array using the slice() method.

See the following example:\

var lang = ['php','java','c','c#','.net', 'python'];
var res = lang.slice(0,4);
console.log(res); // ["php", "java", "c", "c#"]

The res array contains the first 4 elements of the lang array. The original array lang remains entire.

Conclusion

In this tutorial, you have learned JavaScript array slice() method and how to use it.

More JavaScript Tutorials

Recommended:-JavaScript Arrays

Be First to Comment

Leave a Reply

Your email address will not be published.