将嵌套数组转换为对象

时间:2015-12-01 13:09:12

标签: javascript arrays json eval

我正在谈论的API在一个非常奇怪的嵌套数组结构中返回它的注册表。我想将这个怪物转换成一个对象,这样我的应用程序就可以轻松访问存储在此输出中的整个对象。

API给我的输出如下所示:

[ 
    [ "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" ]
]

每个数组中的最后一个值表示条目的值,在最后一个条目之前的所有内容都与用于保存值的键相加。由于大小限制,我不能只在那里删除大的json编码对象,所以这不是一个可行的解决方法。

我现在做了一个非常肮脏和缓慢的解决方案,涉及2个eval()。 (我知道......这是一个禁忌,所以我正在寻找一个更好的解决方案)我猜这可以加载更快,但我无法弄清楚如何......

下面的代码片段使用angular,因为我的应用程序是基于Angular的,但我对任何快速/干净的解决方案持开放态度。一个香草js方法或一些巧妙使用像lodash或下划线的东西将是非常受欢迎的。

我的肮脏和缓慢的解决方案

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 ){
                    //not last of array, so must be a key
                    keys += '[\'' + value + '\']';
                    // check if the object already exists
                    if( !angular.isDefined( eval('registry' + keys) ) ){
                        eval('registry' + keys + ' = {}'); 
                    }
                }else{ 
                	//last one in this entry, must be the value
                  	eval('registry' + keys + ' = \'' + value + '\''); 
               	}
                
            });        
        });
        
        console.log('registry final');
        console.log(registry);
        $scope.registry = registry;
    }
}
<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>

7 个答案:

答案 0 :(得分:4)

这是一个符合您需求的解决方案。另外,请永远不要使用eval。 JavaScript中总有一种更好的方式。

您可以根据自己的使用情况调整以下代码。

var 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" ]
];

var o = {};

data.forEach(function(a) {
  var keys = a.slice(0, a.length-2);
  var cur = o;

  keys.forEach(function(k) {
    if (cur[k] == null) {
      cur[k] = {};
    }
    cur = cur[k];
  });

  cur[a[a.length-2]] = a[a.length-1]
});

output.innerHTML = JSON.stringify(o, null, 2);
<pre id='output'></pre>

答案 1 :(得分:2)

一种紧凑的解决方案,可以避免计算数组中的值位置。

var array = [
        ["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"]
    ],
    obj = {};

array.forEach(function (a) {
    var p = obj,
        v = a.pop(),
        k = a.reduce(function (r, b) {
            p[r] = p[r] || {};
            p = p[r];
            return b;
        });
    p[k] = v;
});

document.write('<pre>' + JSON.stringify(obj, 0, 4) + '</pre>');

答案 2 :(得分:1)

基本上你只是对它们进行循环并创建嵌套对象。你不需要为此使用eval。你不应该使用它的原因有很多。性能,安全性,可调试性(https://www.nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood/

var asObject = {}
//loop over them
data.forEach(function(val) {
    //create the top level object that matches the key if it doesn't exist
   if (!asObject.hasOwnProperty(val[0])) {
    asObject[val[0]] = {};
   }
   //store it 
   var theHolder = asObject[val[0]];
   //loop over all the middle elements creating nested object 
   for (var index = 1; index < val.length - 2; index++) {
       var element = val[index];
       if (!theHolder.hasOwnProperty[element]) {
           theHolder[element] = {};
       } 
       theHolder = theHolder[element]
   }
    //the last one is the value, so just set it
    var lastKey = val[val.length - 2];
    theHolder[lastKey] = val[val.length - 1];
});

console.log(asObject);

答案 3 :(得分:0)

var someObj = $scope.data.reduce(function(accum, array) {
    var value = array.pop(); //pulls last item off of array

    //takes the remaining items and condenses them into 1 string
    var key = array.reduce(function(acc, str) {
        return acc + str;
    }, '');

    accum[key] = value;
    return accum;
}, {}); //the empty object in this line is the seed value

每个子数组都获得处理并传递给空对象种子,然后将其分配给someObj

答案 4 :(得分:0)

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 len = entry.length, tmp = registry;
            for (var i = 0; i < len - 1; i++) {
                key = entry[i];
                if (i < len - 2) {
                    if (!tmp[key]) {
                      tmp[key] = { };
                    }
                    tmp = tmp[key];
                } else {
                    tmp[key] = entry[i + 1];
                }
            }
        });
        console.log('registry final');
        $scope.registry = registry;
    }
}
<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()">
    {{ registry }}
  </div>
    
</div>

答案 5 :(得分:0)

这是使用递归完成的:

$scope.registry = $scope.data.reduce(function register(registry, entry) {
    var key = entry[0];
    if (entry.length === 2) {
        registry[key] = entry[1];
    } else {
        registry[key] = register(registry[key] || {}, entry.slice(1));
    }
    return registry;
}, {});

答案 6 :(得分:0)

以下是基于@Jared Smith的解决方案的另一种选择。在他的解决方案中,键被嵌入到浅地图中的字符串键中。这将创建我的其他解决方案的嵌套对象结构。

如果您是array.reduce()的新手,请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

var someObj = array.reduce(function(previousVal, currentVal) {
    //strip off the value to use at the end
    var value = currentVal.pop();

    //create all the nested objects
    currentVal.reduce(function(acc, str, idx, arr) {

        if (idx !== arr.length - 1 ) {
            if (!acc.hasOwnProperty(str)) {
                acc[str] = {};
            }
            return acc[str];    
        } else {
            //the last one in the array is the key for the value
            acc[str] = value;
            return;
        }

    }, previousVal);
    return previousVal;
}, {}); 

console.log(someObj);