convert array to json

0

I have this array

  

var array = [4,5,7,80,4,52,15,5];

and I want to convert it to this format

[
 [[2.0924907914914139, 2.3946516145034309], 1],
 [[2.4682273904177849, 1.6516482666336787], 1],
 [[1.9754558657999959, 1.9355779157529831], 1],
 [[2.082637117528451, 2.1142405395800332], 1]
]

therefore it would be:

[
 [[4, 5], 1],
 [[7, 80], 1],
 [[4, 52], 1],
 [[15, 5], 1]
]

can this type of data be inserted into an array, I mean insert an integer and an array?

    
asked by hubman 15.11.2016 в 15:11
source

1 answer

1

As I understand you, an arrangement with a certain amount of PAR elements (8 in the example you give), and you want to put these elements together in groups of a 2, in another arrangement. In turn, the arrangement that contains each pair, you want to put into another, which has a second element of value = 1.

In short, you can go through the arrangement, take the values of pairs, and place them in a resulting arrangement:

var arreglo=[4,5,7,80,4,52,15,5];
var retorno = new Array();
arreglo.forEach(function(currentValue,index,arr) {
    if(index % 2 == 0 && arreglo.length > (index + 1)) //Me quedo solo con las posiciones pares, que tengan un elemento delante.
    {
        retorno.push([[arreglo[index], arreglo[index + 1]], 1]);
    }
});
    
answered by 15.11.2016 / 15:56
source