It was very difficult for me to describe my problem in the title. I've already looked at the "solution" but it solves it in a different way than I was trying, and I'm really interested if it was possible to do it my way. Turns out I have this:
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["JavaScript", "Gaming", "Foxes"]
}
];
And I must make a function that entering the parameter of "firstName" (for example, Akira) and a property (for example likes) responds with the values corresponding to the property of the firstName that was entered (that is, in the case of setting Akira and likes parameters, the function responds with ["Pizza", "Coding", "Brownie Points"].
In the case that the property does not exist, the function returns "No such property". In case the entered firstName does not exist, the function returns "No such contact".
function lookUpProfile(name, prop){
// Only change code below this line
for (let i = 0; i < contacts.length; i++){
if (contacts[i].firstName == name && (contacts[i].hasOwnProperty(prop)) == true) {
return contacts[i][prop]
}
else if (contacts[i].firstName !== name){
return "No such contact"
}
else if (contacts[i].hasOwnProperty(prop) == false ){
return "No such property"
}
}
}
Here is the function that I have made that works but only if it matches that the parameters to be entered are those of the first object as Akira (that is, contacts[0]). Why, as far as I thought, loops are useful because they repeat themselves, but nevertheless, since contacts[0] is found in the first iteration, the loop does not continue, I would like it to continue until contacts[1] so that it finally ends up with the correct firstName.
For that I have tried to wrap that loop in another but the same problem continues. Can someone clear up this confusion I'm having with loops and if adding some detail can actually be done so that the loop doesn't stop and go to contacts[1] and actually check all the objects.
As @DavidSilva tells you, you have a problem in the way you are evaluating your conditions.
ISSUE
Given a
array
that contains objects, you want to perform a search on each element of thearray
using as a filter 2 values that have been passed as arguments to the search function. The elements of the array have a common format: they are objects with a key-value pair. The search parameters are set in such a way that the first parameter is the value offirstName
an object's property, and the second parameter is the name of some property of the object.The function must return the value associated to the second parameter (property) in case it gets a match, or the message "No such property". In case a match is not found, it should return the message "No such contact".
SOLUTION
To achieve the goal we are going to use a loop
foreach
to iterate through the elements of ourarray
. We will compare the input parameter with the value ofelement.firstName
. In case of getting a match, we will search the element if there is a property with the name that we have passed as the second parameter to the search, for this we will use thehasOwnProperty(property)
. If the property exists we return its value, if the property does not exist we return the appropriate message. If the search does not return a result, we must return the appropriate message.We will create a function called (as the OP proposes)
lookUpProfile(firstName, property)
, to which we pass 2 parameters.The code can be as follows:
I hope this is the answer you are looking for.
NOTE
As in your question you ask for a clarification of the use of the loop
for
and I have used the methodfor of
of aArray
, I will make the following dimension.A loop
for
as we know it is designed, among other things, to loop through an iterable array or object. Aarray
is an iterable object. Therefore we can go through its elements accessing the position index of each one of them.In your question, you state that you want to be able to traverse the entire
array
or at least continue the search if you don't find a match in the first element. As @DavidSilva told you, you have a problem in the way you evaluate your condition.A way to make use of the loop
for
for this example would be the following:The result is the desired. You should notice that I have used the statement
break
to exit the loop. I do it so that the search stops. What if 2 or more contacts have the same name? Our loopfor
will return the first one it finds. If we don't use the statementbreak
, then the for loop will run to the end and our function will return the last match it finds.I hope that clarifies your doubt.