Handle JSON by Javascript

0

After ajax, my return from php is:

[{"1":"4"},{"2":"3"},{"3":"7"}]

Data is variable in quantity and content.

I need to treat it with javascript and convert it into an array, in this format:

var retorno = [
    [1, 4],
    [2, 3],
    [3, 7]
];

I'm trying:

var parsed = JSON.parse(response);
var arr = [];
for(var x in parsed){
    arr.push(parsed[x]);
}
alert(arr);

response is the variable with return data

But the result of the alert is: [object Object],[object Object],[object Object]

    
asked by anonymous 29.08.2017 / 03:56

2 answers

2

I created an array within for by entering the key and key value. See:

var parsed = JSON.parse('[{"1":"4"},{"2":"3"},{"3":"7"}]');
var arr = [];
for(var x in parsed){    
  arr.push([parseInt(x)+1, parseInt(parsed[x][parseInt(x)+1])]);
}

console.log(arr);
    
29.08.2017 / 04:24
1

I think something like this

var response = '[{"1":"4"},{"2":"3"},{"3":"7"}]';
var parsed = JSON.parse(response);
var arr = [];
parsed.forEach(function(pvalue,index,ar){
    for(var pname in pvalue){
        arr.push([pname,pvalue[pname]]);
    }
});
console.log(arr);
    
29.08.2017 / 04:25