[英]How to filter array based on selected objects itself Java script
How to filter an array in Javascript by object value for the below example:对于以下示例,如何通过 object 值过滤 Javascript 中的数组:
x = [1,2,3,4,5,6,7,8,9,10];
expected if I selected values
Start = 1;
End = 5;
Filtered array to be numbers between 1 to 5
newArray1 = [1,2,3,4,5];
and if I selected the below values如果我选择了以下值
Start= 6;
End= 9;
Expected to get this values newArray2= [6,7,8,9];期望得到这个值 newArray2= [6,7,8,9];
NOTE: This need to be applied to use for clock hours and minutes to set schedule and durations and create booking slots.
comments inline内联评论
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // filter the arr based on start/end values const filter = (arr, start, end) => arr.filter((val) => val >= start && val <= end); console.log(filter(x, 1, 5)); console.log(filter(x, 6, 9)); // if results need be based on indexes const get = (arr, start, end) => arr.slice(start - 1, end); console.log(get(x, 1, 5)); console.log(get(x, 6, 9));
It's built into modern day JavaScript它内置于现代 JavaScript
x = [1,2,3,4,5,6,7,8,9,10];
function getRange(x, start, end) {
return x.filter(c=> c>= start&& c <= end)
}
console.log(getRange(x, 1, 5), getRange(x,6,9))
Assuming your array has only number values假设您的数组只有数字值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.