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

144
Visualizações
How to run some element in recyle from an array?

Well, I know my question is very difficult. But I want to know Things?

Suppose, I have an array element?

const array = [
   "AEDAUD", "AEDCAD", "AEDCHF", "AEDEUR", "AEDGBP", "AEDINR", "AEDJPY", "AEDNOK", "AEDNZD", "AEDPKR", "AEDSAR", "AEDSEK", "AEDZAR", "ANG", "ARSBRL", "ARSEUR", "ARSGBP"
]

I need here on Function that can run only 10 element every time. When I run function for first time it's show me first 10 element. Then If I run again then it show me the rest element.

Suppose this is function

const runElement = () => {
    console.log(element)
}

If I run this function first time it show me first 10 element from array.

"AEDAUD"
"AEDCAD"
"AEDCHF"
"AEDEUR"
"AEDGBP"
"AEDINR"
"AEDJPY"
"AEDNOK"
"AEDNZD"
"AEDPKR"

Then If I run that function again it should show me the rest element-

"AEDSAR"
"AEDSEK"
"AEDZAR"
"ANG"
"ARSBRL"
"ARSEUR"
"ARSGBP"

If I run the function again it should go first 10 element again. In that It should run for always.

Is this possible in javascript?

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

0

Maybe you would be interested in using a generator: provide it with the array and the chunk size, and let it just cycle happily through the array for ever while yielding the results.

The iterator can be used to grab as many results as needed:

function* pullValues(arr, chunkSize) {
    let i = 0;
    while (true) {
        yield arr.slice(i, i += chunkSize);
        if (i >= arr.length) i = 0;
    }
}

// demo

const array = [
   "AEDAUD", "AEDCAD", "AEDCHF", "AEDEUR", "AEDGBP", "AEDINR", "AEDJPY", "AEDNOK", "AEDNZD", "AEDPKR", "AEDSAR", "AEDSEK", "AEDZAR", "ANG", "ARSBRL", "ARSEUR", "ARSGBP"
];
let it = pullValues(array, 10);

console.log(...it.next().value);
console.log(...it.next().value);
console.log(...it.next().value);
console.log(...it.next().value);
console.log(...it.next().value);
console.log(...it.next().value);
console.log(...it.next().value);
//....

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a variable to store a flag and the function can reference to this variable to output differently.

const array = [
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "10",
    "11",
    "12",
    "13",
    "14",
    "15",
    "16",
    "17",
];

let isShowFirstTenItem = true;
const show = () => {
    let output;
    if (isShowFirstTenItem) {
        output = array.slice(0, 10);
    } else {
        output = array.slice(10, -1);
    }
    isShowFirstTenItem = !isShowFirstTenItem;
    console.log(output);
};

show();
show();
show();

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do something like this. Here tracker is used to keep track of what elements are displayed already. If tracker is 0, nothing is displayed yet, if 1 it means, first ten is already displayed. For loop only displays 10 elements once.

const array = [
   "AEDAUD", "AEDCAD", "AEDCHF", "AEDEUR", "AEDGBP", "AEDINR", "AEDJPY", "AEDNOK", "AEDNZD", "AEDPKR", "AEDSAR", "AEDSEK", "AEDZAR", "ANG", "ARSBRL", "ARSEUR", "ARSGBP"
]

let tracker = 0;

const display = () => {
  for(let i = tracker*10; i<10*tracker + 10 ; i++){
    if(!array[i]) break;
    console.log(array[i])
  }
  if(tracker >= array.length){
    tracker = 0;
  } else {
    tracker = tracker+1
  }
}

display()
display()
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