我从Javascript中的JSON文件中获取一系列视频持续时间(以秒为单位),为简化起见,它将如下所示:
array = [30, 30, 30]
Run Code Online (Sandbox Code Playgroud)
我想将每个值添加到前一个值,直到满足条件(总和小于变量x),然后获取要播放的视频数组中的新值和索引位置.
例如,如果x = 62(条件),我想要添加数组中的前两个值(从我的理解,reduce()在这里是合适的),并且index = 2(数组中的第二个视频).
我已经掌握了reduce():
var count = array.reduce(function(prev, curr, index) {
console.log(prev, curr, index);
return prev + curr;
});
Run Code Online (Sandbox Code Playgroud)
但似乎无法超越这一点..谢谢
我正在迭代以下类型的 JSON 对象:
"entries": [{
"first_name": "Brigitte",
"last_name": "Bardot",
"country": "Paris, France",
"profession": "Actress"
},
{
"first_name": "Apollo",
"last_name": "AA",
"country": "Witten, Germany",
"profession": "Writer"
}]
Run Code Online (Sandbox Code Playgroud)
使用 v-for 和 lodash 我有一个按字母顺序排列的这些条目的列表:
<div v-for="user in orderedUsers" :key='user' class="user">
<p>{{ user.first_name }} {{ user.last_name }} ({{ user.country }}), {{ user.profession }}</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我想创建一个包含姓氏的第一个字母的标题,以便按字母顺序对条目进行分组,如下所示:
A
阿波罗
乙
芭铎
ETC..
我用来对条目进行排序的函数如下:
computed: {
orderedUsers: function () {
return orderBy(this.text[0].entries, 'last_name')
}
}
Run Code Online (Sandbox Code Playgroud)
有什么简单有效的方法来实现这一目标的想法吗?