你好,我有一个包含大范围值的数组,我需要从该数组中获取值范围并做XMLHttpRequest我已经做过的例子:
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);
我想要实现的是如何获取前5个值,然后获取接下来的5个值,依此类推
答案 0 :(得分:0)
您可以使用此数组的.slice方法
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
答案 1 :(得分:0)
尝试一下:
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); }