我正在尝试在我的项目中实现以下代码:http://jsfiddle.net/wQysh/351/。
除了该行,一切都很好:
t = _.findWhere(sc, { id : Number(a.trim()) });
他们使用了underscorejs,我想将其翻译为JQuery而不使用另一个库。
我仔细阅读了文档,并说:
findWhere_.findWhere(列表,属性)
浏览列表并返回与属性中列出的所有键值对匹配的第一个值。
如果找不到匹配项,或者列表为空,则将返回未定义。
但是我仍然对此感到困惑,因为我不确定确切返回什么(作为第一个值)。谁能给我一个替代该行的JQuery吗?
提前致谢..
最佳答案
如果您没有_.findWhere()
的一般性质,则可以使用简单的while
循环,并将id与(fiddle)的数值进行比较:
t = 0; // t is used as a counter
aValue = Number(a.trim()); // assign the value to a variable instead of iterating it
while (t < sc.length && sc[t].id !== aValue) { t++; }; // find the index where the id is the as the aValue
t < sc.length && toSet.push(sc[t]); // if t is less the sc.length we found the item in the array
如果需要不带下划线的
findWhere
,请尝试此gist。