我尝试了以下代码,我希望获得最新日期的记录。 在以下示例中,我需要具有最新和相同日期的记录 带键2和5的平均数组。 我试过以下但没有成功。
var arr = [];
arr['0'] = { label : '2003', Date : '2003-11-17 00:00:00'};
arr['1'] = { label : '2007', Date : '2003-11-17 00:00:00'};
arr['2'] = { label : '2009', Date : '2010-11-17 00:00:00'};
arr['3'] = { label : '2008', Date : '2005-11-17 00:00:00'};
arr['4'] = { label : '2008', Date : '2005-11-17 00:00:00'};
arr['5'] = { label : '2010', Date : '2010-11-17 00:00:00'};
console.log(arr);
var max = arr.reduce(function(a, b) {
return (moment(a.Date).isAfter(b.Date));
}, 0);
console.log(max);
答案 0 :(得分:4)
首先根据日期索引数组
var dateMap = {};
arr.forEach( function(item){
dateMap[ item.Date ] = dateMap[ item.Date ] || [];
dateMap[ item.Date ].push( item );
});
现在对键进行排序,简单的字符串排序就可以了
var keys = Object.keys( dateMap );
keys.sort();
由于数组按升序排序,因此获取最大值为
的最后一个键var maxDate = keys.pop();
现在获取与此最大日期相关联的所有数组项
return dateMap[ maxDate ];
答案 1 :(得分:2)
您可以对数组进行排序和过滤:
private void bindHandler() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
callNext();
Log.e("In HANDLER------>", "HANDLER");
}
}, DISPLAY_LENGTH * 1000);
}
private void callNext() {
if (counter < statusModel.getStories().get(position).getUserStories().size()) {
if (statusModel.getStories().get(position).getUserStories().get(counter).getType().equalsIgnoreCase("photo")) {
Log.e("PHOTOOOO--->", "PHOTOOOOO");
String photo = statusModel.getStories().get(position).getUserStories().get(counter).getFileName();
image.setVisibility(View.VISIBLE);
video_view.setVisibility(View.GONE);
Glide.with(getApplicationContext())
.load(photo)
.into(image);
counter++;
DISPLAY_LENGTH = Long.parseLong(statusModel.getStories().get(position).getUserStories().get(counter).getDisplayTime());
Log.e("DIAPLAY TIME--->", String.valueOf(DISPLAY_LENGTH));
bindHandler();
} else {
if (statusModel.getStories().get(position).getUserStories().get(counter).getType().equalsIgnoreCase("video")) {
Log.e("VIDEOOOO--->", "VIDEOOOOO");
image.setVisibility(View.GONE);
video_view.setVisibility(View.VISIBLE);
String VideoURL = statusModel.getStories().get(position).getUserStories().get(counter).getFileName();
uri = Uri.parse(VideoURL);
if (!TextUtils.isEmpty(VideoURL)) {
video_view.setVideoURI(uri);
video_view.requestFocus();
video_view.start();
video_view.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
}
});
video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Log.e("VIDEooooooo--->", "Completeeeee");
Log.e("Thread Notifyyyy--->", "Hooo Gayaaaaaa");
counter++;
callNext();
}
});
}
}
}
}
}
&#13;
var arr = [];
arr['0'] = { label : '2003', Date : '2003-11-17 00:00:00'};
arr['1'] = { label : '2007', Date : '2003-11-17 00:00:00'};
arr['2'] = { label : '2009', Date : '2010-11-17 00:00:00'};
arr['3'] = { label : '2008', Date : '2005-11-17 00:00:00'};
arr['4'] = { label : '2008', Date : '2005-11-17 00:00:00'};
arr['5'] = { label : '2010', Date : '2010-11-17 00:00:00'};
var max = arr
.sort((a, b) => moment(a.Date).isAfter(b.Date))
.filter((elem, index, arr) => elem.Date === arr[arr.length - 1].Date);
console.log(max);
&#13;
答案 2 :(得分:1)
这个简单的reduce
函数将通过将当前日期与保留的最大值进行比较(使用累加器存储最大值)来完成工作。对于非常大的数组,这应该比排序和过滤更快,因为它只需要访问一次数组。
var max = arr.reduce((max, d) => max > d.Date ? max : d.Date, 0);
更详细代码中的等效版本:
var max = arr.reduce(function(max, d) {
if (max > d.Date) {
return max;
} else {
return d.Date;
}
}, 0);
如果需要完整值,可以使用对象作为累加器(注意对初始累加器值的更改):
var max = arr.reduce((max, d) => max.Date > d.Date ? max : d, {Date: 0});
或
var max = arr.reduce(function(max, d) {
if (max.Date > d.Date) {
return max;
} else {
return d;
}
}, {Date: 0});
答案 3 :(得分:0)
您可以使用array#reduce
获取具有最大日期的对象,然后使用array#filter
获取该日期的所有对象。
var arr = [];
arr['0'] = { label : '2003', Date : '2003-11-17 00:00:00'};
arr['1'] = { label : '2007', Date : '2003-11-17 00:00:00'};
arr['2'] = { label : '2009', Date : '2010-11-17 00:00:00'};
arr['3'] = { label : '2008', Date : '2005-11-17 00:00:00'};
arr['4'] = { label : '2008', Date : '2005-11-17 00:00:00'};
arr['5'] = { label : '2010', Date : '2010-11-17 00:00:00'};
var max = arr.reduce(function(prev,cur){
return moment(prev.Date).isAfter(moment(cur.Date)) ? prev : cur;
});
var result = arr.filter(function(obj){
return max.Date === obj.Date;
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
答案 4 :(得分:0)
使用Math.max
,map
和filter
的其他解决方案。
var arr = [];
arr['0'] = { label: '2003', Date: '2003-11-17 00:00:00' };
arr['1'] = { label: '2007', Date: '2003-11-17 00:00:00' };
arr['2'] = { label: '2009', Date: '2010-11-17 00:00:00' };
arr['3'] = { label: '2008', Date: '2005-11-17 00:00:00' };
arr['4'] = { label: '2008', Date: '2005-11-17 00:00:00' };
arr['5'] = { label: '2010', Date: '2010-11-17 00:00:00' };
//Get max year
var year = Math.max.apply(null, arr.map(function(e) { return new Date(e.Date.replace(" ","T")).getFullYear() }));
//Filter array based on year
console.log(arr.filter(function (a,b) { return new Date(a.Date.replace(" ","T")).getFullYear() === year }))