2
實際上,問題與typeahead引導 有關,因爲我需要使用自動完成來定義要在輸入文本中顯示的值數組。過濾一組對象以獲得數組值
無論如何,目標只是定義一個讀取對象數組並返回一個字符串數組的函數。 這是我的代碼(1)。
(1)的目標是: 1)從對象數組中獲取字符串數組。 2)過濾這個數組,拒絕一些元素。
它不起作用,因爲我想拒絕的元素在數組中繼續存在。 事實上,在自動完成中,我得到了錯誤的值,實際上它破壞了代碼,因爲錯誤並不是預期的。
應該如何修復代碼並改進它?
(1)
element.typeahead({
source: function ({
var users = _.map(app.userCollection.models, function (model) {
if (model.get('id') === app.currentUser.id) {
return false;
}
return model.get('first_name') + ' ' + model.get('last_name');
});
console.log(users); // [false, 'some name'];
_.reject(users, function(name) {
return name === false;
});
console.log(users); // [false, 'some name'];
// why does the false value persist?
return users;
}
});
它有幫助,除了我想獲得模型'model.id!== app.currentUser.id;'而不是'model.id === app.currentUser.id;'。非常感謝。 –
好吧,一個小技巧,你明白了 – jakee