我正在尝试使用客户端的名称来获取客户端的价值,并且由于某种原因我得到了完整的客户端对象:
function createBank(clients) {
return {
clients: clients,
safeBoxValue: function() {
return this.clients.reduce(function(sum, client) {
return sum + client.value;
}, 0);
},
getclientValue: function(clientName) {
return this.clients.find(function(client) {
if (client.name === clientName) {
return client.value;
}
});
}
}
}
var clients = [
{name: "John", value: 349},
{name: "Jane", value: 9241},
{name: "Jill", value: 12734},
]
var bank = createBank(clients);
bank.safeBoxValue(); // 22324
bank.getclientValue('Jill'); // {"name":"Jill","value":12734}
有人知道为什么吗?谢谢!
答案 0 :(得分:3)
select T1.user2_id, message, user_profile.user_name, user_profile.avatar, max(cdate) message_date from (select messages.receiver_id user2_id, messages.message, (message_date) cdate from messages where messages.sender_id=2 group by messages.receiver_id union distinct(select messages.sender_id user2_id, messages.message, max(message_date) cdate from messages where messages.receiver_id = 2 group by messages.sender_id)) T1 inner join user_profile on (user_profile.id = T1.user2_id) group by T1.user2_id order by message_date desc
通过传入一个返回 Boolean 值的函数来确定该对象是否是您正在寻找的对象。尽管有代码,但您的函数仍在工作,因为当您返回array.find()
时,您返回的值是“真实的”。
如果您刚刚完成此操作,该功能将完全相同:
client.value
它会遍历数组,直到你返回true然后传递给你的元素,在这种情况下,你刚刚发现整个对象。要获得该值,您需要单独返回:
getclientValue: function(clientName) {
return this.clients.find(function(client) {
return client.name === clientName
});
}
请记住getclientValue: function(clientName) {
var found = this.clients.find(function(client) {
return client.name === clientName
});
return found && found.value
}
只返回找到的第一个值。