Page 1 of 1

Ayuda con array

Posted: Thu Dec 07, 2017 4:58 am
by djmarkes
Buenas. vuelvo a tener problemas para trabajar con un array.

Llamo a un script en mi servidor que me genera los resultados en un array.

Tengo este codigo para la llamada:

Code: Select all

BeginJS
var url="http://xxxx.com/script.php";
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
    if (request.readyState === 4) {
        if (request.status === 200) {
            document.body.className = 'ok';
            $rootScope.res = request.responseText;
        } else {
            document.body.className = 'error';
        }
    }
};
request.open("GET", url , false);
request.send(null);
EndJS
Todo correcto, se almacena el resultado en la variable [res] de esta forma, este es el contenido:

[["12","46541.66"],["405","11729.68"],["176","16588.84"],["945","110162.39"],["477","21640.98"],["583","28344.36"],["1096","85405.80"],["749","24210.74"],["582","40753.60"],["1342","47796.64"],["2193","24647.16"],["492","47661.21"],["996","16505.49"],["2074","50377.71"],["900","167387.68"],["513","93502.17"],["1349","26585.16"],["136","48308.66"],["1021","22238.08"],["1031","19497.86"],["1265","23086.81"],["275","11875.26"],["35","17678.97"],["317","30919.68"],["194","25981.78"],["885","40977.53"],["20","22154.17"],["1945","29541.48"],["319","36430.83"],["540","38644.07"],["746","17850.38"],["1117","45460.98"],["998","16983.13"],["1429","69794.37"],["691","19534.23"],["1671096","11418.30"],["981","39643.50"],["167513","10346.96"],["1463","5077.05"],["327","33550.56"]]

Necesito acceder a esos datos y no se como, por ejemplo el primer elemento de ese array sería:

elemento: 12 valor: 46541.66
el siguiente:
elemento: 405 valor: 11729.68

Puede alguien guiarme un poco?

Gracias!

Re: Ayuda con array

Posted: Wed Dec 13, 2017 3:48 am
by luishp
No entiendo muy bien como está almacenada la información en la variable [res]
¿[res] es un Array?
Si es así, ¿que hay en [res(1)] por ejemplo?
¿Las dobles comillas, las comas y los corchetes están almacenados dentro de la variable también?

Re: Ayuda con array

Posted: Wed Dec 13, 2017 11:39 am
by Gaev
djmarkes:

The php script appears to return an "array of arrays" ... assuming that every inner array contains two elements (items), try this code ...

Code: Select all

BeginJS
var url="http://xxxx.com/script.php";
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
    if (request.readyState === 4) {
        if (request.status === 200) {
            document.body.className = 'ok';
	    var myResponse = request.responseText;
	    responseString = myResponse.join();
	    alert(responseString);
            $rootScope.res = responseString;
        } else {
            document.body.className = 'error';
        }
    }
};
request.open("GET", url , false);
request.send(null);
EndJS
The NeoBook variable [res] will contain the contents of the "array of arrays" that is flattened into a comma separated set of values ...
something like ...
12 , 4641 , 405 , 11729.68 , 176 , 16588.84 etc. etc.
... which should make it easy for you to extract the values (in pairs of two).

If the content displayed by the alert command is correct, you can remove it; otherwise, post what your experience was.

Note that if you want to extract these values on the javascript side, you can use ...

Code: Select all

for (i = 0; i < myResponse.length; i++) {
    var thisElem = myResponse[i];
    alert("Element:" + [i] + " = " + thisElem[0] + " ... " + thisElem[1]);
}