Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

93
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda