两个值之间的结果数组 - javascript
results array between two values - javascript
我想知道是否有办法通过另一个数组获取具有最小值和最大值的数组,或者类似这样的方法:
var numbers = ['10','15','20','25','30','35','40','45','50'];
var getnumbers = function(min,max){
//return a result that is push to another array, in this case "results"
};
getnumbers(10,30);
console.log(results);
输出结果应该是 10 - 15 - 20 - 25 - 30
您可以这样做:numbers.filter(function (value) { return value >= 10 && value <= 30 });
如果你真的想要输出N1 - N2 - N3 ... - NN
,只需要numbers.filter(function (value) { return value >= 10 && value <= 30 }).join(' - ');
试试这个
var numbers = ['10','15','20','25','30','35','40','45','50'];
var output = "";
var getnumbers = function(min,max){
for (var i = 0; i < numbers.length; i++){
if(numbers[i] >= min && numbers[i] <= max){
output += numbers[i] + "-";
}
}
};
getnumbers(10,30);
console.log(output);
一种略有不同的方法,具有用于过滤的自定义回调函数。
function filterNumbers(min, max) {
return function (a) { return a >= min && a <= max; };
}
var numbers = ['10', '15', '20', '25', '30', '35', '40', '45', '50'],
result = numbers.filter(filterNumbers(10, 30));
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
JavaScript 有解决这个问题的内置函数。
let numbers = ['10','15','20','25','30','35','40','45','50'];
// Get all elements between '10' and '30'
let result = numbers.splice(numbers.indexOf('10'),numbers.indexOf('30') + 1);
console.log(result);
// If you want the answer to literally be '10-15-20-25-30'
let resultString = "";
for(const i in result){
resultString.concat(`${result[i]}-`)
}
console.log(resultString);
我想知道是否有办法通过另一个数组获取具有最小值和最大值的数组,或者类似这样的方法:
var numbers = ['10','15','20','25','30','35','40','45','50'];
var getnumbers = function(min,max){
//return a result that is push to another array, in this case "results"
};
getnumbers(10,30);
console.log(results);
输出结果应该是 10 - 15 - 20 - 25 - 30
您可以这样做:numbers.filter(function (value) { return value >= 10 && value <= 30 });
如果你真的想要输出N1 - N2 - N3 ... - NN
,只需要numbers.filter(function (value) { return value >= 10 && value <= 30 }).join(' - ');
试试这个
var numbers = ['10','15','20','25','30','35','40','45','50'];
var output = "";
var getnumbers = function(min,max){
for (var i = 0; i < numbers.length; i++){
if(numbers[i] >= min && numbers[i] <= max){
output += numbers[i] + "-";
}
}
};
getnumbers(10,30);
console.log(output);
一种略有不同的方法,具有用于过滤的自定义回调函数。
function filterNumbers(min, max) {
return function (a) { return a >= min && a <= max; };
}
var numbers = ['10', '15', '20', '25', '30', '35', '40', '45', '50'],
result = numbers.filter(filterNumbers(10, 30));
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
JavaScript 有解决这个问题的内置函数。
let numbers = ['10','15','20','25','30','35','40','45','50'];
// Get all elements between '10' and '30'
let result = numbers.splice(numbers.indexOf('10'),numbers.indexOf('30') + 1);
console.log(result);
// If you want the answer to literally be '10-15-20-25-30'
let resultString = "";
for(const i in result){
resultString.concat(`${result[i]}-`)
}
console.log(resultString);