[英]How to filter array based on selected objects itself Java script
對於以下示例,如何通過 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];
如果我選擇了以下值
Start= 6;
End= 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.
內聯評論
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));
它內置於現代 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))
假設您的數組只有數字值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.