1

In my code json returning format like this

 var data = [
     {"field1":"918666400041","field2":"2016-05-19 11:52:52"},
     {"field1":"918666400035","field2":"2016-05-19 11:52:52"},
     {"field1":"918666400060","field2":"2016-05-19 11:52:52"}
 ];

But I need convert to array format with out keys like:

 var Data = [
     ['918666400041', '2016-05-19 11:52:52'],
     ['918666400035', '2016-05-19 11:52:52'],
     ['918666400035', '2016-05-19 11:52:52']
 ];

Can any one will give solution for this question?

I already tried with angular.forEach function and map function but I didn't get this format.

Michael Doye
  • 8,063
  • 5
  • 40
  • 56
Ram
  • 11
  • 3
  • what is the idea behind ignoring the field names of an object? If you want JSON to be like this then you should not have gone for object itself. What you expect is plain array of arrays. – NightsWatch Jun 14 '16 at 12:43

6 Answers6

2

 var obj = [ {"field1":"918666400041","field2":"2016-05-19 11:52:52"}, {"field1":"918666400035","field2":"2016-05-19 11:52:52"}, {"field1":"918666400060","field2":"2016-05-19 11:52:52"} ];

var res = obj.map(function(val) {
   return [val.field1, val.field2]
})

console.log(res)
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
2

Depending on the browsers you intend to support, you could just use the ES5 map function:

var mappedData = data.map(function(obj) { 
    return [obj.field1, obj.field2]; 
});

If you don't have access to the ES5 array methods, you could use angular's forEach as follows:

var mappedData = [];
angular.forEach(data, function(obj) { 
    mappedData.push([obj.field1, obj.field2]); 
});
net.uk.sweet
  • 12,444
  • 2
  • 24
  • 42
1

try this

var data = [
{"field1":"918666400041","field2":"2016-05-19 11:52:52"},
{"field1":"918666400035","field2":"2016-05-19 11:52:52"},
{"field1":"918666400060","field2":"2016-05-19 11:52:52"}
];

var x =[];

angular.forEach(data,function(val,index){
x.push(val.field1)
x.push(val.field2)
})

Now in x you will get required array

Vicky Kumar
  • 1,358
  • 1
  • 14
  • 26
0

You could try the reduce function:

var Data = data.reduce(function(previousValue, currentValue) {
    previousValue.push([currentValue.field1, currentValue.field2])
    return previousValue;
}, []);
Hopeful Llama
  • 728
  • 5
  • 26
0

You can so something like:

var l_IN = <your starting JSON> ;

var I_OUT = [] ;

for (x = 0 ; x < l_IN.length ; x++) {

    dummy = {l_IN.field1,l_IN.field2} ;

    l_OUT.push(dummy) ;

// or l_OUT.push(l_IN.field1,l_IN.field2) ;
}

....
FDavidov
  • 3,505
  • 6
  • 23
  • 59
0

You can use something like this:

var data = [ {"field1":"918666400041","field2":"2016-05-19 11:52:52"},{"field1":"918666400035","field2":"2016-05-19 11:52:52"}, {"field1":"918666400060","field2":"2016-05-19 11:52:52"} ], 
    newData = [];

// Iterating through the data array, to process object by object
for (var i = data.length - 1, tempArray; i >= 0; i--) {
    // returning a new object with only the current object value for each key
    tempArray = Object.keys(data[i]).map(function(k) { return data[i][k] });
    newData.push(tempArray); 
}

console.log(newData);

This is quite similar (but a bit more complicated here) than Converting JSON Object into Javascript array

Community
  • 1
  • 1
remborg
  • 528
  • 3
  • 14