1

I'm converting json object to an array. The data I get is like below. I have try google but not found the answer. How to solve this issue from data below

[{"january":"0.00","february":"0.00","mac":"1271.00","april":"5.00","may":"0.00","june":"0.00","july":"0.00","august":"0.00","september":"0.00","october":"0.00","november":"0.00","december":"0.00"}]

into this

$scope.data = [
        [0.00, 0.00, 1271.00, 5.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00]
      ];

in file.html

<div class="card">
    <div class="card-header">
         <i class="icon-graph icon-bg"></i>Bar Chart
    </div>
    <canvas id="bar" class="chart chart-bar" chart-data="graph" chart-labels="labels"></canvas>

</div>

in file.js

$http.get(commonData.apiURL + 'dashboard/countDataGraph.php')
        .success(function (data) {
            $scope.graph = data;
            console.log(JSON.stringify(data));

        })
        .error(function (data, status, headers, config) {
            $scope.errorMessage = "Couldn't load the list of Orders, error # " + status;
            console.log("error");
    });
d_matteo
  • 39
  • 6

3 Answers3

2

Object properties in JavaScript are unordered, so solutions that use Object.values() can offer no guarantees about the resulting array being correctly ordered.

You have to get the individual values out in the right order, and construct a new array out of them. Pre-defining an array of months, combined with a map() operation will do the trick:

const months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
const data = [{"january":"0.00","february":"0.00","march":"1271.00","april":"5.00","may":"0.00","june":"0.00","july":"0.00","august":"0.00","september":"0.00","october":"0.00","november":"0.00","december":"0.00"}];

const result = data.map(v => months.map(m => v[m]));
       
console.log(result);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Do you know if `Object.getOwnPropertyNames()` iterates the object in order? I have read it from [here](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties), but i'm still confused if some method can traverse or not an object in order of keys creation. – Shidersz Apr 02 '19 at 03:24
  • @Shidersz If have given up trying to figure it out. There are all kinds of edge cases and older browsers will not follow the ECMAScript 2015/2016. If I need to rely on order, I use a `Map`. – Robby Cornelissen Apr 02 '19 at 03:27
  • When I use code that having => my controller will be error. How to fix that? – d_matteo Apr 02 '19 at 03:32
  • `data.map(function(v) { return months.map(function(m) { return v[m]; })});` But seriously, if you want to support browsers that old, you should look into BabelJS or something similar to transpile. – Robby Cornelissen Apr 02 '19 at 03:37
  • 1
    okay thanks @RobbyCornelissen . Its working fine now! Really appreciated. – d_matteo Apr 02 '19 at 03:45
-1

You can use the function Object.values

Object.values(o) // Returns an array of values.

let obj = [{"january":"0.00","february":"0.00","mac":"1271.00","april":"5.00","may":"0.00","june":"0.00","july":"0.00","august":"0.00","september":"0.00","october":"0.00","november":"0.00","december":"0.00"}];
let values = obj.map(Object.values);
console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75
  • This answer completely ignores the fact that object properties in JavaScript are unordered. You might get your results back in the correct order, you might not. It's completely dependent on the JavaScript engine implementation. – Robby Cornelissen Apr 02 '19 at 03:09
-1

You can use map() and Object.values

const arr = [{"january":"0.00","february":"0.00","mac":"1271.00","april":"5.00","may":"0.00","june":"0.00","july":"0.00","august":"0.00","september":"0.00","october":"0.00","november":"0.00","december":"0.00"}]

const res = arr.map(x => Object.values(x));

console.log(res);
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73