Divide arrays knowing the length that needs to be obtained

1

I am making a request through a API of the NASA , to obtain information, but the data returned through this is long, because each corresponds to each day of the year.

It depends on the year, whether it's a leap year (366 days) or not (365 days) , I get information, which I access, are objects and arrays , but because it is very long, I did not know that the maximum length allowed was 100 values within array .

Array

  

(365) [5.34, 5.68, 5.59, 5.52, 5.42, 4.32, 4.48, 3.68, 3.93, 4.79, 5.74, 4.26, 3.2, 5.26, 5.34, 4.97, 5.21, 5.13, 5.3, 5.07, 5.26, 4.99 , 5.36, 5.26, 4.54, 5.99, 6.18, 5.96, 5.47, 4.9, 4.19, 5.66, 5.17, 5.53, 4.88, 5.12, 5.65, 5.38, 5.06, 4.9, 5.64, 5.01, 4.4, 5.44, 5.88, 6.3, 5.5 , 6.38, 6.48, 6.58, 6.08, 6.49, 5.4, 6.15, 6.66, 6.67, 6.23, 5.8, 6.26, 5.6, 6.25, 4.85, 3.73, 4.49, 3.69, 3.43, 5.09, 4.64, 5.73, 5.48, 5.22, 5.72 , 6.65, 5.47, 6.63, 6.15, 6.22, 6.03, 6.58, 6.28, 6.24, 3.22, 2.16, 5.69, 5.72, 5.02, 5.86, 5.22, 4.53, 5.81, 4.86, 6.41, 6.51, 6.45, 6.67, 6.44, 6.61 , 5.42, 5.16, 6.21, ...]

[0 … 99]
[100 … 199]
[200 … 299]
[300 … 364]
length: 365

The vector ends by dividing into 4 vectors, 3 with 100 values each, and a last one with 64. I would like to know how to obtain the values depending on the number of days per month, taking them as a constant reference.

Example

January : (31 days)

The idea is to take the first 31 values of the first array [0 ... 99] , and be able to assign them, that is, only take these values, [0 .. .30] .

    
asked by Pedro Miguel Pimienta Morales 16.04.2018 в 05:20
source

2 answers

1

First, the array always maintains its original length. In the console it is presented in groups of 100, but that is a detail that you can ignore.

What I would do would be to build an arrangement of months with your name and number of days. If 366 data come in the response, set the month of February to 29 days.

Then I do a forEach on the month array and within the loop I remove as many elements of the array as days I have the month using Array.splice .

var meses=[
{nombre:'ene',dias:31},
{nombre:'feb',dias:28},
{nombre:'mar',dias:31},
{nombre:'abr',dias:30},
{nombre:'may',dias:31},
{nombre:'jun',dias:30},
{nombre:'jul',dias:31},
{nombre:'ago',dias:31},
{nombre:'sep',dias:30},
{nombre:'oct',dias:31},
{nombre:'nov',dias:30},
{nombre:'dic',dias:31}
];
var respuesta = [7.65, 4.71, 8.93, 8.04, 2.91, 5.32, 5.34, 3.19, 5.70, 2.48, 5.21, 6.98, 9.48, 2.52, 7.9, 5.69, 3.0, 9.58, 6.35, 3.99, 2.16, 2.02, 4.83, 7.39, 1.08, 8.18, 4.89, 2.09, 2.43, 1.96, 7.2, 1.71, 5.70, 6.8, 8.03, 2.20, 2.71, 9.11, 1.45, 2.82, 8.12, 2.59, 5.64, 6.03, 4.76, 8.24, 8.00, 9.7, 9.88, 4.98, 7.36, 8.14, 8.29, 2.61, 2.7, 6.14, 4.38, 5.13, 1.74, 4.37, 8.75, 5.67, 1.13, 9.07, 7.95, 9.67, 1.67, 8.83, 1.20, 7.15, 5.74, 6.48, 1.72, 1.11, 4.67, 8.73, 7.64, 3.70, 8.55, 3.02, 1.75, 3.00, 8.2, 3.41, 2.41, 4.09, 7.24, 5.17, 4.4, 7.7, 9.99, 2.93, 6.96, 4.77, 2.53, 4.52, 6.1, 8.39, 1.07, 7.94, 7.73, 6.27, 7.42, 6.44, 6.30, 6.63, 2.96, 6.72, 4.45, 1.47, 7.95, 6.76, 6.35, 3.66, 4.6, 9.82, 5.55, 7.87, 5.35, 7.93, 2.68, 3.69, 4.47, 2.22, 9.00, 6.92, 1.00, 5.31, 4.09, 7.24, 6.92, 5.06, 4.66, 7.72, 3.04, 9.05, 6.2, 8.21, 6.50, 9.81, 2.99, 4.06, 5.2, 5.4, 7.32, 4.78, 1.64, 5.87, 3.81, 5.02, 5.50, 6.37, 3.26, 6.10, 3.54, 5.29, 2.14, 6.05, 3.69, 5.96, 7.97, 4.49, 9.69, 4.48, 4.20, 1.30, 4.7, 8.98, 7.73, 1.68, 1.87, 8.97, 9.9, 1.41, 8.86, 2.12, 1.73, 7.87, 5.78, 5.69, 6.71, 9.70, 9.81, 5.24, 7.82, 7.27, 3.12, 7.79, 1.94, 4.74, 8.4, 8.91, 9.4, 7.75, 8.41, 5.03, 5.63, 8.91, 1.62, 5.56, 6.66, 5.14, 1.80, 4.93, 3.60, 2.21, 1.20, 6.13, 6.47, 2.08, 5.23, 9.98, 9.01, 2.56, 9.43, 7.96, 6.59, 3.63, 4.07, 4.27, 5.27, 5.60, 8.15, 9.89, 7.81, 9.1, 9.73, 4.53, 2.39, 9.6, 9.07, 9.14, 7.37, 3.94, 8.61, 6.72, 1.60, 8.02, 8.6, 7.71, 9.78, 4.63, 5.40, 4.93, 7.54, 4.19, 9.49, 1.44, 2.48, 3.67, 5.72, 2.25, 5.99, 1.10, 3.53, 4.70, 6.44, 3.13, 6.12, 4.78, 4.87, 1.24, 6.12, 4.90, 5.35, 8.75, 8.24, 6.96, 5.66, 5.5, 7.19, 6.04, 8.28, 4.87, 2.92, 3.80, 9.33, 8.24, 2.40, 7.68, 4.87, 1.06, 6.09, 5.05, 1.88, 5.55, 5.70, 2.62, 1.70, 9.78, 7.74, 7.06, 9.49, 2.19, 7.67, 1.70, 3.94, 4.48, 1.58, 1.10, 3.84, 6.38, 2.39, 7.74, 9.53, 8.00, 5.99, 4.06, 6.3, 2.14, 6.44, 7.23, 9.09, 1.68, 5.43, 4.87, 5.94, 1.12, 8.69, 1.62, 4.89, 2.22, 8.1, 6.59, 6.51, 7.5, 3.0, 2.80, 8.07, 2.93, 5.81, 6.47, 1.35, 7.87, 7.77, 3.77, 3.38, 5.15, 7.94, 6.2, 2.69, 6.49, 8.89, 4.41, 8.19, 9.02, 7.52, 7.04, 9.2, 9.54, 3.9, 9.89, 3.77, 3.55, 7.49, 7.62, 4.54, 3.33, 5.79, 8.23, 3.97, 9.17, 6.16, 9.42, 9.03, 4.41];

if(respuesta.length===366) {
meses[1].dias=29;
};

meses.forEach(function(mes) {
 var dias=mes.dias;
 mes.datos=respuesta.splice(0,dias);
 mes.cant_datos=mes.datos.length;
});

console.log(meses);
    
answered by 16.04.2018 / 13:53
source
1

You can use the Array#slice Javascript function, which should indicate the index where you want to start and the length of the array you need to obtain.

Example:

var enero = vector.slice(0, 31)
//enero tendrá los 31 primeros valores del vector original
    
answered by 16.04.2018 в 05:54