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

134
Visualizações
How to create a function in typescript which takes array as an user input and how to find array length?

I need to create a function in typescript that takes array as an input that accept any type of array. and then calculate the number of elements present in it and return it.

I want a output like let if user input : ["cricket","football"] then output : 2

how to solve this this using typescript?

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

0

you can use the generic type T to allow any type of array in parameter

arrayLength<T>(myArray:T[]): number {
    return myArray.length;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

One way to do it, is like so:

// We tell ts to expect an array of any type as parameter and return a number
function arrLength(someArray : any[]) : number {
    return someArray.length;
}

// Test array with a length of 6
const arr = ['a', 'b', 'c', 1, 2, 3];

// Should output 6
console.log(arrLength(arr));
about 4 years ago · Juan Pablo Isaza Relatório

0

You can check this tutorial out on how to pass an array to a function:>> https://www.tutorialspoint.com/typescript/typescript_passing_arrays_to_functions.htm

To calculate the length of an array, it'll be like this:

console.log(array.length);

Detailed documentation:>> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length

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