// Why doesn't this:
_.invoke(['D','C'], 'lastIndexOf', ['A','B','C'])

// Return this:
[-1,2]?


我有绳子。 (输入)

“ ABC”

拆分为数组。 (InputBuffer)

['A','B','C']

我也有一个包含任意字符的数组。 (TriggerChars)

['D','E']

我想检查InputBuffer中的最后一项,以查看是否匹配任何TriggerChars。

我想在InputBuffer中获得两个TriggerChars的最后一次出现。

_.invoke(['D','E'], 'lastIndexOf', ['A','B','C']);
// Returns [-1,-1] since C isn't D or E.


_.invoke(['D','C'], 'lastIndexOf', ['A','B','C']);
// Why doesn't this return [-1,2]

_.lastIndexOf(['A','B','C'],'D') == -1
_.lastIndexOf(['A','B','C'],'C') == 2


我对Invoke没什么了解?
http://underscorejs.org/#invoke

最佳答案

您需要的是:

_.map(['D', 'C'], function (x) { return _.lastIndexOf(['A', 'B', 'C'], x)})

09-30 16:37
查看更多