简体   繁体   中英

how can I match a username with the key of a property in an object which is in an array?

I have an array in which objects are included. I want to get the username from client and check whether that username exists in keys of objects or not. But I don't know how to match that username with the keys of objects!

Here is a simple form of my code :

var usersArray = [
    {
        name : "Simon",
        password : 123
    },
    {
        name :"David",
        password : 456
    }
];

To check if an array contains an item, you can use Array.prototype.some , Array.prototype.every , Array.prototype.find , or a for loop.

Here's all 4 ways:

 var usersArray = [ { name : "Simon", password : 123 }, { name :"David", password : 456 } ]; const userNameFromClient = 'David'; // use a `for of` loop (using a `while`, or a regular `for` loop would also work) // A loop allows us to iterate through an entire collection. // Once we find an item satisfying a condition, we can signal that. // This is a good starting point that explicitly // shows the mechanics of looping over a collection // to find an item satisfying a condition for (const user of usersArray) { if (user.name === userNameFromClient) { console.log(true); } } // Since this operation is quite frequent in programming, // most languages give us tools and methods to achieve // above in a more declarative fashion and with less code // For your particular case, the most common approach // would be to use the `some` method on the array. // We want to return true if there exists at least one item // in our array that satisfies our condition // Code bellow will return true if one object in `usersArray` // has a property `name` whose value is a string // equal to the string contained in the // `userNameFromClient` variable console.log( usersArray.some(user => user.name === userNameFromClient) ) // Another option is to use the find method. // Since `find` will return the actual user object // that satisfies a condition instead of a boolean `true` or `false`, // we would have to convert the returned result // into a Boolean ourselves console.log( Boolean(usersArray.find(user => user.name === userNameFromClient)) ) // The `some` method also has a cousin called `every` // which can be used in this situation but is a little // bit messy because it requires multiple negations // You can see what I mean if we explain the operation // in plain English: // If not every item in a given array does not satisfy a condition // return true // Above is just a convoluted way of saying if there exists // at least one item satisfying a condition, return true // Code bellow will return true if one object in `usersArray` // has a property `name` whose value is a string // equal to the string contained in the // `userNameFromClient` variable console.log( !usersArray.every(user => user.name !== userNameFromClient) )

 var usersArray = [{ name: "Simon", password: 123 }, { name: "David", password: 456 }]; let name = "David"; let result = usersArray.some(x => x.name === name) console.log(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM