【发布时间】:2015-10-21 10:14:46
【问题描述】:
我有一个多维对象。我需要转换为数组的嵌套对象,我是这样做的:
var foobar = {"foo1": { "5": "bar1", "8": "bar2" }, "foo2": { "3": "bar8", "5": "bar9" }};
angular.forEach(foobar, function(value, key) {
if (angular.isObject(value)) {
var arr = Object.keys(value).map(function(k) { return value[k]; });
foobar[key] = arr;
}
});
console.log(foobar);
{"foo1": ["bar1", "bar2"], "foo2": ["bar8", "bar9"]};
如何将对象转换为数组保留索引,使其如下所示:
{"foo1": [5 => "bar1", 8 => "bar2"], "foo2": [3 => "bar8", 5 => "bar9"]};
【问题讨论】:
标签: javascript arrays angularjs object multidimensional-array