Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
How to create a nested list by sending data from a function

I have the next code:

let dataList = [];

function filli(index, name, words){
    dataList[index] = index;
    dataList[index] = name;
    dataList[index] = words;
}

filli(0, "David", "Testing this")
filli(1, "John", "My cellphone")
console.log(dataList)

I get in console the next:

$ node attempt.js
[ 'Testing this', 'My cellphone' ]

And my expected output in console would be:

[[0, "David", "Testing this"], [1, "John", "My cellphone"]]

But I don´t get that in console, as you can see I was trying to get that data in my filli function, sending an index, name, and words but it doesn't work.

I hope you can help me, thank you.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You are overriding existing values when you do dataList[index]= .... You need to add them as an array, something like this:

let dataList = [];

function filli(index, name, words){
    dataList[index] = [index, name, words];
}

filli(0, "David", "Testing this")
filli(1, "John", "My cellphone")
console.log(dataList)
about 4 years ago · Juan Pablo Isaza Relatório

0

You are overriding the same value:

let dataList = [];

function filli(index, name, words){
    dataList[index] = [] // first create new array
    dataList[index][0] = index; // then assign to proper indexes
    dataList[index][1] = name;
    dataList[index][2] = words;
}

filli(0, "David", "Testing this")
filli(1, "John", "My cellphone")
console.log(dataList)
about 4 years ago · Juan Pablo Isaza Relatório

0

Ideally functions should return a value which your function doesn't at the moment. An alternative solution would be to add your arguments to an array and return that, and then push that result into the dataList array.

const dataList = [];

function filli(index, name, words){
  return [index, name, words];
}

dataList.push(filli(0, 'David', 'Testing this'));
dataList.push(filli(1, 'John', 'My cellphone'));

console.log(dataList);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda