Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!