Note that there are some explanatory texts on larger screens.

plurals
  1. POA good JavaScript to add/remove items from/to array?
    primarykey
    data
    text
    <p>folks! Today I created this script that has the following functionality:</p> <ul> <li>add new items to array</li> <li>list all items from the array</li> <li>remove an item from the array</li> </ul> <p>There are two functions:</p> <ul> <li>addToFood() - adds the value of input to the array and updates innerHTML of div </li> <li>removeRecord(i) - remove a record from the array and updates innerHTML of div</li> </ul> <p>The code includes 3 for loops and you can see it at - <a href="http://jsfiddle.net/menian/3b4qp/1/" rel="noreferrer">http://jsfiddle.net/menian/3b4qp/1/</a></p> <p>My Master told me that those 3 for loops make the solution way to heavy. Is there a better way to do the same thing? Is it better to decrease the loops and try to use splice? Thanks in advance.</p> <p><strong>HTML</strong></p> <pre><code>&lt;!-- we add to our foodList from the value of the following input --&gt; &lt;input type="text" value="food" id="addFood" /&gt; &lt;!-- we call addToFood(); through the following button --&gt; &lt;input type="submit" value="Add more to food" onClick="addToFood();"&gt; &lt;!-- The list of food is displayed in the following div --&gt; &lt;div id="foods"&gt;&lt;/div&gt; </code></pre> <p><strong>JavaScript</strong></p> <pre><code>var foodList = []; function addToFood () { var addFood = document.getElementById('addFood').value; foodList.push(addFood); for (i = 0; i &lt; foodList.length; i++) { var newFood = "&lt;a href='#' onClick='removeRecord(" + i + ");'&gt;X&lt;/a&gt; " + foodList[i] + " &lt;br&gt;"; }; document.getElementById('foods').innerHTML += newFood; } function removeRecord (i) { // define variable j with equal to the number we got from removeRecord var j = i; // define and create a new temporary array var tempList = []; // empty newFood // at the end of the function we "refill" it with the new content var newFood = ""; for (var i = 0; i &lt; foodList.length; i++) { if(i != j) { // we add all records except the one == to j to the new array // the record eual to j is the one we've clicked on X to remove tempList.push(foodList[i]); } }; // make redefine foodList by making it equal to the tempList array // it should be smaller with one record foodList = tempList; // re-display the records from foodList the same way we did it in addToFood() for (var i = 0; i &lt; foodList.length; i++) { newFood += "&lt;a href='#' onClick='removeRecord(" + i + ");'&gt;X&lt;/a&gt; " + foodList[i] + " &lt;br&gt;"; }; document.getElementById('foods').innerHTML = newFood; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload