Arrays_how to set more values in an Array?

Asked

Viewed 83 times

-2

I’m starting now in Js and I have something I don’t know how to do, Whenever the "store" function is executed, I need it to take values from an input, create new positions in an array, and store the values in this new positions, it would be more like the following :

let agregado;
let armazena;
let i;

function armazenador() {
    agregado = document.querySelectorAll('.entNots input');
    let a = 0;
    for (i = 0; i < (i + 5); i++) {
        armazena[i] = agregado[a];
        a++;
    }
}

whenever it was executed, it would fill 5 more positions without overlapping the previous ones, and need to have no limitations on number of free positions.

1 answer

0


var x = 0;
let armazena=[];
let agregado = document.querySelectorAll("input.entNots");

function armazenador()
{
for (const val of agregado) {
    armazena.push(val.value);
}

 x++;

 elementosAdicionados();
}

function elementosAdicionados()
{
   var e = [];   
    
   for (var y=0; y<armazena.length; y++)
   {
     e.push(armazena[y]);
   }
   console.log(e);
}
<input class="entNots" value="">
<input class="entNots" value="">
<input class="entNots" value="">
<input class="entNots" value="">
<input class="entNots" value="">

<input type="button" id="button1" value="Add" onclick="armazenador();"></input>
 

Document.querySelectorAll - returns all elements in the document that correspond to the specified CSS selectors, in the class case entNots

push () - adds new items at the end of an array and returns the new length of that array.

for-of - This loop is specific to iterate between the elements of a list. It traverses iterative objects, calling a custom function with instructions to be executed for the value of each distinct object.

Syntax:

for (variavel of iteravel){
    declaração;
}

Where:

variável: at each iteration, a value of a different property is assigned to the variable;

iteravel: object whose attributes will be iterated.

Example:

 let menu = ['segunda - Feijão tropeiro', 'terça - macarronada', 'quarta - Baião de dois', 'quinta - Feijoada', 'sexta - CHOPADA'];
    
for (let value of menu) {
  console.log(value);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.