【发布时间】:2017-09-25 17:36:09
【问题描述】:
如何使用带有起始索引的 lodash 的 takeRightWhile 从数组中获取值?
这里的重点是我想从一个特定的起点向后迭代,直到满足某个参数。
我想做的例子:
const myArray = [
{val: 'a', condition: true},
{val: 'b', condition: false},
{val: 'c', condition: true},
{val: 'd', condition: true},
{val: 'e', condition: true},
{val: 'f', condition: true},
{val: 'g', condition: false},
];
const startAt = 5;
const myValues = _.takeRightWhile(myArray, startAt, {return condition === true});
// --> [{val: 'c', condition: true}, {val: 'd', condition: true}, {val: 'e', condition: true}]
我查看了文档 https://lodash.com/docs/4.17.4#takeRightWhile 并不能确定这是否可行。
是否有更好的方法来做到这一点?
【问题讨论】:
标签: javascript arrays lodash