Well, you have an array of profiles and you need to check each one. How are you going to do that other than looping? You could also use a for...of loop - that might even be a little “better” - if you’re comfortable with it. You could use while or do while but I think those are not as good. You could also do something like an array prototype method - but those are still looping, it’s just that the looping is hidden from you.
But I think a standard for loop is the most basic way to solve this, especially at the level at which I am assuming you are.
Does that make sense? We are using a for loop because we need to check each profile in the array, one by one. A standard/basic way to do that is to loop though the indexes and use them to index into the array as we check them.
For example:
for (var i = 0; i < contacts.length; i++) {
console.log('profile # =', i, ' --- first name =', contacts[i].firstName)
}
Those check two different things. Either could work in various situations.
In the first example:
if (contacts.firstName === name)
You are asking if contacts has a property that matches exactly name. But in this case contacts is an array, so perhaps you meant:
if (contacts[i].firstName === name)
That is asking if that array has an element at the index i that matches name.
The second one:
if (contacts[i].firstName)
is asking if that array has an element at index i and that that element has a property called firstName that is defined with a truthy value. In other words, it will fail if that value is not define or has one of the 8 (OK, 9) falsy values in JS. Since we’re talking about strings here, we’re asking if it is defined and is not an empty string. That is a perfectly valid thing to check in certain situations. i don’t know if it needs to be checked here.