JavaScript Get a number of elements from a specified starting index number elements from,number elements in periodic table,number elements

Let's say I have a simple array like this: var myArr = [0,1,2,3,4,5,6,7,8,9]

I'd like to extract a number of elements, starting from a specific index, like this:

myArr.getElementsFromIndex(index, numberOfElements)

where, unlike .slice(), if we hit the last index, elements from the start of the array should be returned instead (so that the total number of elements returned will always be respected). Either pure javascript or a library like underscore/lodash can be used.

Examples:

myArr.getElementsFromIndex(3, 5) 

should return[3,4,5,6,7] and

myArr.getElementsFromIndex(8, 5) 

should return [8,9,0,1,2]

Answer:1

Array#slice takes a start and end index (not a start index and a number of elements).

Array#splice does what you want, except for the wrapping around (but also modifies the original array).

You can write a wrapper function using slice (which will not modify the original array):

function getElementsFromIndex(arr, start, numElements) {
    if(start + numElements > arr.length) {
        var endOfArr = arr.slice(start, arr.length);
        var elementsFound = arr.length - start;
        var restElements = getElementsFromIndex(arr, 0, numElements - elementsFound);
        return endOfArr.concat(restElements);
    }
    return arr.slice(start, start + numElements);
}

This function returns what you require (see example), and even wraps around multiple times, if needed.

If you want to tie the function to arrays, in order to use it as you propose (ie. myArr.getElementsFromIndex(start, numElements)), you can add it to Array's prototype. You might want to look up arguments for/against modifying prototypes of built-in types, though.

Array.prototype.getElementsFromIndex = function(start, numElements) {
    if(start + numElements > this.length) {
        var endOfArr = this.slice(start, this.length);
        var elementsFound = this.length - start;
        return endOfArr.concat(this.getElementsFromIndex(0, numElements - elementsFound));
    }
    return this.slice(start, start + numElements);
};

See example of the last one here.

Answer:2

Use the below code

var myArr = [0,1,2,3,4,5,6,7,8,9];

function getElementsFromIndex(startIndex, num) {

    var elems = [];
    for(var iter = 0; iter<num; iter++) {
        if(startIndex >= myArr.length) {
            while(startIndex >= myArr.length) {
               startIndex -=  myArr.length;
            }
        } 
        elems.push(myArr[startIndex]);
        startIndex++;
    }
    return(elems);
}
Answer:3

Add this to your js code:

Array.prototype.getElementsFromIndex = function (start, len) {
    var newArray = [],
        origArray = this,
        i = start;

    while (newArray.length < len) {
        newArray.push(origArray[i++]);

        if (i >= origArray.length)
            i = 0;
    }
    return newArray;
}

You can use it exactly the way you wanted:

var myArr = [0,1,2,3,4,5,6,7,8,9];
alert(myArr.getElementsFromIndex(8, 5));

JSFIDDLE DEMO: http://jsfiddle.net/x6oy0krL/1

Maybe some people will say that it is not right to extend objects like Array, documentElement and so on, but the result here is as the OP wanted.

I want to say that the original array will not be modified, too.

Answer:4

I'm very new to JavaScript (and coding) and I'm studying through the book Head First JavaScript. From reading, I thought an anonymous function could be an argument since functions are values, but I ...

I'm very new to JavaScript (and coding) and I'm studying through the book Head First JavaScript. From reading, I thought an anonymous function could be an argument since functions are values, but I ...

I have created a hyperlink from my report and pass through a value successfully. ="http://Applicationlive:8080/ApplicationName/secure/EventReportPage.zul?EventReportId=" & Fields!event_report_id....

I have created a hyperlink from my report and pass through a value successfully. ="http://Applicationlive:8080/ApplicationName/secure/EventReportPage.zul?EventReportId=" & Fields!event_report_id....

  1. open hyperlink from excel
  2. open hyperlink from outlook
  3. open hyperlink from excel in chrome
  4. open hyperlink from vba
  5. open hyperlink from powerpoint
  6. open hyperlink from outlook in chrome
  7. open hyperlink from pdf
  8. open hyperlink from excel vba
  9. open hyperlink from cell vba
  10. open hyperlink from access
  11. open hyperlink from word
  12. open hyperlink from ppt
  13. open hyperlink command
  14. open hyperlink from html
  15. open from hyperlink
  16. open hyperlink in new tab
  17. open hyperlink in new tab html
  18. open hyperlink in chrome
  19. open hyperlink in new window
  20. open hyperlink with specific browser

$localStorage.doctorDateTime.push({ fullDate : new Date(doctorDateTime) }); I passed date string in new Date and then save it to local storage but when i retrieve ...

$localStorage.doctorDateTime.push({ fullDate : new Date(doctorDateTime) }); I passed date string in new Date and then save it to local storage but when i retrieve ...

let assume that I want to create directive that matched only for element that match amInput[type=dropdown] how can I do that? I can for example: .directive('amInput',function () { return { ...

let assume that I want to create directive that matched only for element that match amInput[type=dropdown] how can I do that? I can for example: .directive('amInput',function () { return { ...

  1. what can create stress
  2. what can create sediment
  3. what can create an electric field
  4. what can create a magnetic field
  5. what can create a black hole
  6. what can create earthquakes
  7. what can create errors in calculations
  8. what can create electricity
  9. what can create a hostile work environment
  10. what can create polymers
  11. what can create a sonic boom
  12. what can create hives
  13. what can create with python
  14. what can create carbon monoxide
  15. what can create carbon monoxide in a home
  16. what can create kidney stones
  17. what can create energy
  18. what can create oxygen
  19. what can you create with python
  20. what level can i create a class bo4