Skip to content

How do I fetch only few elements in a list of items/array

This example demonstrates fetching only few elements in a list of items/array.

In the solution shown below, you will see the colors "Green" and "Red" are fetched from an array "["Blue","Green","Red","Yellow"]" using "slice(<first index of element,last index of the element+1>)" javascript method in "storeEval" command and stored into a variable.

Solution:

# Command Target Value
Fetching only few elements from an array
1 storeEval
function f(){ var colors_list = ["Blue","Green","Red","Yellow"]; var colors=colors_list.slice(1,3); return colors[0]+","+colors[1]; }f();
colors

Tips, Tricks, Gotchas & Best Practices:

  • As a best practice, it is always advisable to define the variable names without any spaces and if you want to differentiate the words, use underscores.


Feedback and Knowledge Base