title: Searching through multiple objects in one array using For loop and conditional operators
body: <p>I'm trying to create a function that will search through an array consisting of multiple objects, as shown below, and return the value of one certain prop.</p>
  <pre><code>const contacts = [
    {
      firstName: &quot;Akira&quot;,
      lastName: &quot;Laine&quot;,
      number: &quot;0543236543&quot;,
      likes: [&quot;Pizza&quot;, &quot;Coding&quot;, &quot;Brownie Points&quot;],
    },
    {
      firstName: &quot;Harry&quot;,
      lastName: &quot;Potter&quot;,
      number: &quot;0994372684&quot;,
      likes: [&quot;Hogwarts&quot;, &quot;Magic&quot;, &quot;Hagrid&quot;],
    },
    {
      firstName: &quot;Sherlock&quot;,
      lastName: &quot;Holmes&quot;,
      number: &quot;0487345643&quot;,
      likes: [&quot;Intriguing Cases&quot;, &quot;Violin&quot;],
    },
    {
      firstName: &quot;Kristian&quot;,
      lastName: &quot;Vos&quot;,
      number: &quot;unknown&quot;,
      likes: [&quot;JavaScript&quot;, &quot;Gaming&quot;, &quot;Foxes&quot;],
    },
  ];
  </code></pre>
  <p>Below are the conditions:</p>
  <ul>
  <li>if Name exists inside the contacts array, then I have to check whether prop exists in that name.</li>
  <li>If the prop doesn't exist, return &quot;No such property&quot;</li>
  <li>If name doesnt exist in the array, return &quot;No such contact&quot;.</li>
  </ul>
  <p>Function name : <code>function lookUpProfile(name, prop)</code></p>
  <p>Now i have previously succeeded in doing this using <code>if else if</code>. Now i want to try it with conditional operators but i can't seem to do it. Below is what i've written</p>
  <pre><code>function lookUpProfile(name, prop) {
    for (let i=0;i&lt;contacts.length;i++) {
        return contacts[i].firstName===name ? contacts[i].hasOwnProperty(prop) ? contacts[i][prop]
          : &quot;No such property&quot;
          : 0
    }
    return &quot;No such name&quot;;
  }
  </code></pre>
  <p>I have followed the if else structures the exact same way i did when i used if else before. But this code can't seem to do the job. Can anybody tell me why?</p>