Home > Mobile >  add or push a 10th index number to the array
add or push a 10th index number to the array

Time:01-15

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

CodePudding user response:

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.

CodePudding user response:

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]));
}

CodePudding user response:

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)

CodePudding user response:

As mentioned by @cybercoder, you probably don't want to change the state variable within the forEach, as that will cause render to be called excessively.

You could simply use a counter and only push elements when the index is divisible by 10:

 let i = 0;
 let {timestamp, size} = this.state;

 result.forEach(element => {
     if (i % 10 === 0) {
         timestamp.push(element.timestamp);
         size.push(element.size);
     }
     i  ;
  });

  this.setState({
    ...this.state,
    timestamp,
    size
  });

If you do not want to include the very first (index 0) element:

 let i = 0;
 let {timestamp, size} = this.state;

 result.forEach(element => {
     // Exclude very first element
     if (i % 10 === 0 && i !== 0) {
         timestamp.push(element.timestamp);
         size.push(element.size);
     }
     i  ;
  });

  this.setState({
    ...this.state,
    timestamp,
    size
  });

CodePudding user response:

why dont u try using VECTOR instead?

  •  Tags:  
  • Related