[英]Get Values From Array With Specific Range - Javascript
您好,我有一个包含大范围值的数组,我需要的是从该数组中获取值范围并执行我已经完成的 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 个值等等
你可以使用这个 .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
尝试这个:
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); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.