You should declare the array in the global environment (outside the function). You also have some syntax issue inside the object (should be : instead of =). Then you can simply push() the object into the array in each button click.
Try the following way:
var pessoas = [];
function vai(){
var pessoa = {
nome: document.getElementById('nome').value,
idade: document.getElementById('idade').value
};
pessoas.push(pessoa);
console.log(pessoas);
}
<label>Digite seu nome aqui</label>
<input type='text' id='nome' placeholder='aquii'>
<label>Digite sua idade aqui</label>
<input type='text' id='idade' placeholder='aquii'>
<button onclick='vai()'>
Teste aqui
</button>
You approach without using the push() by maintaining a variable is almost correct except the variable declaration scope. You have to declare that as a global variable. You can assign the object into the current position and increment that in each function call:
var pessoas = [];
var i = 0;
function vai(){
var pessoa = {
nome: document.getElementById('nome').value,
idade: document.getElementById('idade').value
};
pessoas[i] = pessoa;
i++;
console.log(pessoas);
}
<label>Digite seu nome aqui</label>
<input type='text' id='nome' placeholder='aquii'>
<label>Digite sua idade aqui</label>
<input type='text' id='idade' placeholder='aquii'>
<button onclick='vai()'>
Teste aqui
</button>
Please Note: You can also simply use the length property of the array as the current index in which case you do need to maintain the variable:
var pessoas = [];
function vai(){
var pessoa = {
nome: document.getElementById('nome').value,
idade: document.getElementById('idade').value
};
pessoas[pessoas.length] = pessoa;
console.log(pessoas);
}
<label>Digite seu nome aqui</label>
<input type='text' id='nome' placeholder='aquii'>
<label>Digite sua idade aqui</label>
<input type='text' id='idade' placeholder='aquii'>
<button onclick='vai()'>
Teste aqui
</button>