[英]JavaScript remove elements from array until x
I gather an array of times from my back-end [09:00, 09:30, 10:00, 10:30]...
and I would like to filter all times out before and including 12:00. 我从后端
[09:00, 09:30, 10:00, 10:30]...
收集了一些时间[09:00, 09:30, 10:00, 10:30]...
我希望在包括12:00之前过滤所有时间。
How would I go about? 我该怎么办?
I have thought about using an $.each
on the array and checking the strings against each other, but I am not sure if that is the right approach. 我曾考虑在数组上使用
$.each
并相互检查字符串,但我不确定这是否是正确的方法。
This would be the only answer so far that will filter out times if 12:00
is not present in the array: 到目前为止,这将是唯一的答案,如果数组中不存在
12:00
,将过滤掉时间:
var array = ['09:00', '09:30', '10:00', '10:30', '12:00', '12:30', '13:00'];
var result = array.filter(function(e) {
var date = new Date('1.1.2000 ' + e), // create Date object
hours = date.getHours(); // get hours from time
return hours > 12 || (hours === 12 && date.getMinutes() > 0);
});
console.log( result ); // ["12:30", "13:00"]
Alternatively, without relying on Date
object, we can use simple string manipulation: 或者,不依赖于
Date
对象,我们可以使用简单的字符串操作:
var result = array.filter(function(e) {
var date = e.split(':'), // split time by ':'
hours = +date[0]; // get hours as a number
return hours > 12 || (hours === 12 && +date[1] > 0);
});
console.log( result ); // ["12:30", "13:00"]
Another even more straightforward solution is to transform time to a number and filter out everything, that is less or equal to 1200
: 另一个更直接的解决方案是将时间转换为数字并过滤掉所有内容,即小于或等于
1200
:
var result = array.filter(function(e) {
return +e.replace(':', '') > 1200;
});
console.log( result ); // ["12:30", "13:00"]
If the array is ordered, and the value '12:00'
is always present then you can use the index and slice() like 如果数组是有序的,并且值
'12:00'
始终存在,那么你可以使用索引和slice()之类的
var array = ['09:00', '09:30', '10:00', '10:30', '11:00', '11:30']; var newarray = array.slice(array.indexOf('10:00') + 1); snippet.log(JSON.stringify(newarray))
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
If you are not sure about the presents of the value, then you need to iterate over the array and filer 如果您不确定值的呈现,那么您需要遍历数组和文件管理器
var array = ['09:00', '09:30', '10:00', '10:30', '11:00', '11:30']; var newarray = array.filter(function(item) { return item > '10:00' }); snippet.log(JSON.stringify(newarray))
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
This method relies on time strings to always be formatted like this: HH:mm
. 此方法依赖于时间字符串,始终格式如下:
HH:mm
。
It will go through all items in the array and remove the elements that are less or equal to '12:00'
. 它将遍历数组中的所有项,并删除小于或等于
'12:00'
的元素。
var times = ['09:00', '09:30', '10:00', '10:30', '12:30']; for (var i = 0; i < times.length; i++) { if (times[i] <= '12:00') { times.splice(i--, 1); } } document.body.innerHTML = JSON.stringify(times); // Running this code snippet will output ["12:30"]
In which format your data is? 您的数据采用哪种格式? If they are strings and Array is ordered, you could simply find index of element with "12:00" text and slice array from that position.
如果它们是字符串并且数组是有序的,那么您可以简单地找到带有“12:00”文本的元素索引和来自该位置的切片数组。
arr.splice(arr.indexOf('12:00') + 1);
如果存在12.00
:
my_array = backend_array.slice(0, backend_array.index("12.00")+1)
Try using the filter
function: 尝试使用
filter
功能:
var times = ['09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30'] , latestTime = breakUpTime('12:00') , result = '' ; var newTimes = times.filter(function(time){ var thisTime = breakUpTime(time); return thisTime['hour'] < latestTime['hour'] || (thisTime['hour'] == latestTime['hour'] && thisTime['minute'] <= latestTime['minute']); }); function breakUpTime(time) { var parts = time.split(':'); return {'hour' : parseInt(parts[0]), 'minute' : parseInt(parts[1])}; } for(var time = 0; time < newTimes.length; time ++) { result += newTimes[time] + '<br/>'; } document.write(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.