To obtain an array of indexes instead of a string of indexes, you need to use the split()
method on the string:
var indexes = '0,2'.split(','); // Split the string using ',' as the delimiter
Next, you will need to retrieve the fruit values corresponding to each index in the newly created indexes array.
var juicyFruits = []; // Create a new array for the juicy fruits
indexes.forEach(function(index) { // Iterate over the desired fruit indexes
juicyFruits.push(fruit[index]); // Add the delicious fruit :)
});
You now have a new array (juicyFruits
) containing the succulent fruits :)
UPDATE:
Alternatively, a shorter and neater solution (credits to Xotic750)
(the second argument in the map function specifies the context (this))
var indices = '0,2';
var fruitsArray = ['Apple','Banana','Orange'];
fruitsArray = indices.split(',').map(function(index) {
return this[index];
}, fruitsArray);