The API I'm talking to returns a registry in a very odd structure of nested arrays. I want to convert this monster into an object, so my application has easy access to all the objects stored in this release.
The API result gives me the following:
[ [ "settings", "autoLogout", "false" ], [ "settings", "autoLogoutMinutes", "60" ], [ "settings", "presets", "true" ], [ "controller", "rs232", "ip", "192.168.1.11" ], [ "controller", "rs232", "name", "NX-22" ], [ "source", "M23836", "slot1", "ip", "192.168.1.30" ] ]
The last value in each array represents the value of the record; every last one adds the key used to store the value. Due to size limitations, I can't just leave large json-encoded objects there, so this is not a viable workaround.
Now I made a rather dirty and slow solution involving 2 eval (). (I know ... no, no, so I'm looking for a better solution) I guess this can be done faster, but I can't figure out how ...
The snippet below uses angular because my application is based on angular, but I'm open to any quick / clean solution. It would be very welcome the approach of vanilla js or some clever use of something like lodash or underscore.
My dirty and slow solution now
function DemoCtrl($scope){ $scope.data = [ [ "settings", "autoLogout", "false" ], [ "settings", "autoLogoutMinutes", "60" ], [ "settings", "presets", "true" ], [ "controller", "rs232", "ip", "192.168.1.11" ], [ "controller", "rs232", "name", "NX-22" ], [ "source", "M23836", "slot1", "ip", "192.168.1.30" ] ] $scope.init = function(){ var registry = {}; angular.forEach($scope.data, function(entry){ var keys = ''; entry.forEach(function(value, key, entry){ if( key != entry.length - 1 ){
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app> <div ng-controller="DemoCtrl" ng-init="init()"> <pre>{{ registry | json }}</pre> </div> </div>