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

92
Visualizações
add or push a 10th index number to the array

I was working on a chart where i need my data to be sliced into small object based for better visibility. My array that i have is

   {
      "size":[
        {
          "timestamp":"1641329889000",
          "size":12345,
          "fees":123456,
          "cost":168
        },
        {
          "timestamp":"1641387032",
          "size":456789,
          "fees":4567891,
          "cost":249
        },
        {
          "timestamp":"1641435786",
          "size":98765,
          "fees":987654,
          "cost":987
        },
        {
          "timestamp":"1641435786",
          "size":98765,
          "fees":987654,
          "cost":987
        },
        {
          "timestamp":"1641435786",
          "size":98765,
          "fees":987654,
          "cost":987
        }
      ]
    }

in which i want the array to be in this form

{
  "size":{
    "timestamp": ["1641329889000","1641387032","1641435786"],
    "size": [12345,456789,98765],
    "fees": [123456,4567891,987654],
    "cost": [168,249,987]
  }
}

i can achieve this using foreach and push like this

 result.forEach(element => {
     this.state.timestamp.push(element.timestamp);
     this.state.size.push(element.size);
  });

But i want this array to have the items only from the 10,20,30,40th index alone I want not all the value. the values should be chosen only in the basis of x+10 Could anyone help me on this

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

0

You could take a for loop and a step for incrementing the index.

const
    step = 10,
    keys = ["timestamp", "size", "fees", "cost"],
    result = Object.fromEntries(keys.map(k => [k, []]));

for (let i = 0; i < size.lenght; i += step) {
    keys.forEach(key => result[key].push(size[i][key]));
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Using forEach is a waste of resources. You can use for instead:

for(let i=0;i<result.length;i+10){
   this.state.timestamp.push(result[i].timestamp);
   this.state.size.push(result[i].size);
}

For setting the state you should use setState not just push to it.

    let tmp = {
      ...this.state
    }
    for(let i=0;i<result.length;i+10){
       tmp.size.timestamp.push(result[i].timestamp);
       tmp.size.push(result[i].size);
    }
    this.setState(tmp)
about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of forEach why not just use a for loop, and on the condition use the modulus % operator with 10? Like if (i % 10 == 0) inside of the for loop, or just increment i by 10 like i+=10.

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