Get Values From Array With Specific Range - Javascript

121
December 29, 2018, at 6:30 PM

Hello I have an array that contain big range of values , what I need is to get range of values from that array and do XMLHttpRequest example of what I've done already :

var servoangle = [1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2,2.1,2.2....];
var params = "motor1=" + servoangle[0] + "&motor2=" + servoangle[2] + "&motor4=" + servoangle[3] + "&motor5=" + servoangle[4] + "&motor6=" + servoangle[5] + "&motor7=" + servoangle[6] + "&";
var url = "/finaltest2please.php"
var http = new XMLHttpRequest();
http.open('POST', url, true);
//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        console.log(http.responseText);
    }
}
http.send(params);

what I want to achieve is how to get first 5 values then next 5 values and so on

Answer 1

you can use for this .slice method of array

const arr = [1,2,3,4,5,6,7,8,9,10,11,12]; 
console.log(arr.slice(0,5)) // first 5 values 
console.log(arr.slice(5,10)) // next 5 
console.log(arr[10]) // so one 11-th

Answer 2

Try this:

const arr = [0,1,2,3,4,5,6,7,8,9];
const send = (from, to) => {
    let url = '/page.php?';
    let formData = new FormData();
    for (let i=from; i<to; i++) {
        formData.append(`motor${i}`, arr[i])
    }
    fetch(url, {
        method: 'post',
        headers: { 'Content-type': 'application/x-www-form-urlencoded'}
        body: formData
    });
}
for (let i=0; i<arr.length-5; i+=5) { send(i, i+5); }
READ ALSO
Understanding a URL

Understanding a URL

The US EPA offers data to the public through the following web-page: https://wwwepa

61
Flask IMAP application giving incorrect html

Flask IMAP application giving incorrect html

The idea of the application is to serve as an email client, so whatever I get from the email service provider I should be able to display it in my applicationThe application is written in Flask using IMAP to retrieve the emails from the email service...

39
Why do I get a NullReferenceException when using shelljs to clone a git repo?

Why do I get a NullReferenceException when using shelljs to clone a git repo?

I am attempting to write a fairly simple nodejs program that needs to clone a git repo

85
Is it possible to define java-style (non-static) inner class (in class scope)

Is it possible to define java-style (non-static) inner class (in class scope)

For those who are not familiar with java non-static inner classes are classes where the objects has reference to the outer object from which it was created

66