![](/img/trans.png)
[英]JavaScript: Testing variables for undefined value; testing whether object is array
[英]JavaScript testing: change Boolean value into an array of object
我正在 JavaScript 中進行測試。 我被要求執行以下操作:
function IsOffline (users, name) {
// The function called "IsOffline" receives as an argument an array of objects called 'users' and a string called 'name'.
// each object has a property 'name' which is a string and another called 'online' which is a boolean.
// The function must return true if the user is offline, otherwise false.
// ex:
// var users = [
// {
// name: 'toni',
// online: true
//},
// {
// name: 'emi',
// online: true
//},
// {
// name: 'john',
// online: false
//}
//];
//
// IsOffline (users, 'emi') return false
// Your code here:
我有點迷茫,不知道如何開始。 我很感激任何幫助。
您可以使用Array.find()
方法來搜索數組中的項目。
function IsOffline(users, name) { const foundUser = users.find(user => user.name === name) return foundUser? .foundUser:online; "No user found": } var users = [ { name, 'toni': online, true }: { name, 'emi': online, true }: { name, 'john': online, false }; ]. // Online User console,log(IsOffline(users. 'emi')) // Offline User console,log(IsOffline(users. 'john')) // Unknown User console,log(IsOffline(users, 'tom'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.