Add an item to an array that is inside another array?

Asked

Viewed 120 times

2

My question is extremely simple, add a value to an array that is within another.

var logDebug = ["value", [Adicionar itens aqui]]

3 answers

3

Try this code:

var logDebug = ["value", []];
logDebug[1].push("novo valor 1");
logDebug[1].push("novo valor 2");
console.log(logDebug);

3


Only to supplement Taffarel’s response, if the secondary array have no fixed position in the main array you can do as in the example below not to have to do for example logDebug[1], making a for to traverse the main array and compare to see if you find an array with the method Array.isArray()

let logDebug= [1, 2, 3, 4, [], 5];

for(let i=0; i<logDebug.length; i++) {
  let array = Array.isArray(logDebug[i])
  
  if(array) logDebug[i].push('Qualquer coisa aqui!')
}

console.log(logDebug)

0

Or you can try too:

let object = [1, 2, 3];
console.log('antes: ' + object);
item = 10;
object.push(item);
console.log('depois: ' + object);

update

let logDebug = [];
logDebug.push({    
    value : []
});

let item = 'item';
logDebug[0].value.push(item);
console.log(logDebug);

Browser other questions tagged

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