Ad
How Can I Iterate Over Unordered JSON Data And Sort Into An Array
I'm grabbing data from an API which is in a json object structure. The problem is that this json is not in order and I understand is unordered by nature. I was wondering how I could iterate over these keys and sort this data by putting them in an array (I already have them printing out in a table using ng-repeat but they are in random order so my end goal is to have them displayed in order by date). Here is an example of the structure:
{
"01/05/2016": {
"Something1": {},
"Something2": {}
},
"01/01/2016": {
"Something1": {},
"Something2": {}
},
"01/03/2016": {
"Something1": {},
"Something2": {}
}
}
<tr ng-repeat="(key,value) in metrics_data">
<td align="center">{{key}}</td>
//and then I do another ng-repeat right here for values
Ad
Answer
You are right about the fact that orderBy doesn't support object. so just convert the object into array first.
$scope.testObj = {
"01/05/2016": {
"Something1": {},
"Something2": {}
},
"01/01/2016": {
"Something1": {},
"Something2": {}
},
"01/03/2016": {
"Something1": {},
"Something2": {}
}
};
$scope.testObjArray = Object.keys($scope.testObj).map(
function(k) {
return {key: k, value: $scope.testObj[k]}
});
then use orderBy
ng-repeat="obj in testObjArray | orderBy : 'key'"
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad