title: In React, how to add key value to one object in array of objects
body: <p>I'm new to React so please bear with me. currently working on a project and currently stuck at the last part. Per project requirement. I'm to fetch data array from an api. display each object's data on the page. and give every object an input element to be able to add a tag, display tag with the data. then be able to search based on the tag.</p>
  <p>a simple version of the data is something like:</p>
  <pre><code>[{
      id: &quot;1&quot;,
      name: &quot;aaa&quot;,
      info: &quot;xxx&quot;
  },{
      id: &quot;2&quot;,
      name: &quot;bbb&quot;,
      info: &quot;yyy&quot;
  },{
      id: &quot;3&quot;,
      name: &quot;ccc&quot;,
      info: &quot;zzz&quot;
  }]
  </code></pre>
  <p>i first created a newdata array when i fetch data, and added empty string for tags key.</p>
  <pre><code>useEffect(()=&gt;{
    axios.get(apiurl)
      .then(response=&gt;{
          const students = response.data.students;
          const newdata = students.map((o)=&gt;({
                  ...o,
                  tags: []
              }))
          setNewdata(newdata)
      })
  },[apiurl])
  </code></pre>
  <p>so now newdata is like:</p>
  <pre><code>    {
          id: &quot;4&quot;,
          name: &quot;ddd&quot;,
          info: &quot;xyz&quot;,
          tags: []
      }
  </code></pre>
  <p>I'm rendering the data array using the .map() and displaying each Object(student) and displaying all the data which includes the input area to add the tags. below is the part of code that displays the tags and input box that allows users to enter tags:</p>
  <pre><code>newdata.map(function(student, index){ 
    return(
  
  ...//other code before this
  
  &lt;div className=&quot;tagbox&quot;&gt;
      &lt;div className='tags'&gt;
          {student.tags.map(function(tag, index){
              return &lt;span key={index}&gt;{tag}&lt;/span&gt;
          })}
      &lt;/div&gt;
      &lt;input 
          id={student.id}
          type=&quot;text&quot; 
          placeholder='tag'
          value={newtag}
          onChange={e=&gt; setNewtag(e.target.value)}
          onKeyDown={handleKeyDown} /&gt;
  &lt;/div&gt;
  )}
  </code></pre>
  <p>so basically i'm running a handlekeydown function for the enter key. so when the user finishes typing and hits enter. a new tag should display above and have it's value added to the newdata array so i can later match for the search function.
  problem is I'm unable to figure out the correct code and syntax to execute this logic. So Far all i can come up with is this:</p>
  <pre><code>function handleKeyDown(e){
      if (e.key === &quot;Enter&quot;){            
          for (let i = 0; i &lt; newdata.length; i++){                
              if (newdata[i].id == &quot;1&quot;){
                  newdata[i].tags.push(newtag);
              }
          }
          
          setNewtag(''); //this is to empty the input field
      }
  }
  </code></pre>
  <p>as you can see, I'm trying to loop through every object (student) in the data. and when the student id matches. we add the data. else we skip. I've hard coded the value to make it work only on the first student. I've tried many methods but i either add to all or add to none.</p>
  <p>now I've also been able to add an id to each of the input fields in the jsx like so:</p>
  <pre><code>&lt;input id={student.id}/&gt;
  </code></pre>
  <p>now, every input field has a unique id identical to the student's id from the data that's displayed above.</p>
  <p>So the question is how should I change my handlekeydown function so that every time i enter a tag it only adds to the data with the same id.
  PS: every object (student) that's displayed has its own input element from my .map() rendering.</p>
  <pre><code>   //my goal is to type &quot;!@#&quot; into the input#4, hit enter and have
      {
          id: &quot;4&quot;,
          name: &quot;ddd&quot;,
          info: &quot;xyz&quot;,
          tags: [&quot;!@#&quot;]
      }
  </code></pre>