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

274
Visualizações
Javascript - Numeric input to obtain variable length array data

I have a dozen arrays (hypothetically) and each has a completely different length. Is there a way that I can name them such that a numeric input into a function will return an array corresponding to that specific number?

This makes no sense so here's kinda what I want:

var dQw4w9WgXcQ;
const array1 = [1,1,1,1,1,1,1,1,1,1];
const array2 = [1,2,3,4];
const array3 = [1,3,6];

function getArray(id){
    output = constNamed('array' + id);
}

//getArray(2) sets dQw4w9WgXcQ to [1,2,3,4]

and yes, dQw4w9WgXcQ is just something I totally typed on accident

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

0

USING OBJECT

1) You can creat an object that contain all arrays as:

const arrays = {
  array1,
  array2,
  array3,
};

2) Assign the result getArray(2) to dQw4w9WgXcQ by just returning the array from object.

return arrays[`array${id}`];

var dQw4w9WgXcQ;
const array1 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
const array2 = [1, 2, 3, 4];
const array3 = [1, 3, 6];

function getArray(id) {
  const arrays = {
    array1,
    array2,
    array3,
  };
  return arrays[`array${id}`];
}

dQw4w9WgXcQ = getArray(2);
console.log(dQw4w9WgXcQ);

NOTE: If you want to set value to dQw4w9WgXcQ directly after the invocation of the function getArray(2)

var dQw4w9WgXcQ;
const array1 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
const array2 = [1, 2, 3, 4];
const array3 = [1, 3, 6];

function getArray(id) {
  const arrays = {
    array1,
    array2,
    array3,
  };
  dQw4w9WgXcQ = arrays[`array${id}`];
}

getArray(2);
console.log(dQw4w9WgXcQ);

USING ARRAY

var dQw4w9WgXcQ;
const array1 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
const array2 = [1, 2, 3, 4];
const array3 = [1, 3, 6];

function getArray(id) {
  const arrays = [array1, array2, array3];
  return arrays[id - 1];
}

dQw4w9WgXcQ = getArray(2);
console.log(dQw4w9WgXcQ);

about 4 years ago · Juan Pablo Isaza Relatório

0

Have one array contain all the arrays. You can then pass that array into the function as an argument along with the id, and use find to return the array at the index of id - 1 (because indexes are zero-based). If find can't meet the condition it will return undefined.

const arr = [
  [1,1,1,1,1,1,1,1,1,1],
  [1,2,3,4],
  [1,3,6]
];

function getArray(arr, id){
  return arr.find((a, index) => index === id - 1);
}

console.log(getArray(arr, 2));
console.log(getArray(arr, 4));

Note all we're doing here is assigning

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