使用javascript将二维数组创建为多维对象的逻辑
我遇到的情况是我有二维数组,我必须根据它创建一个multidimensiaonl数组
示例:
arr = [[1,2],[3,4,5], [6,7].......n]
我需要像
这样的对象{"1":{"3":{6:{...},7:{...}},"4":{6:{...},7:{...}},"5":{6:{...},7:{...}}},"2":{"3":{6:{...},7:{...}},"4":{6:{...},7:{...}},"5":{6:{...},7:{...}}}}
让我们说
arr = [[1,2],[3,4]]
然后{1:{3,4},2:{3,4}}
if arr = [[1,2],[3,4,5], [6,7]]
然后 结果= {" 1":{" 3":{6,7}," 4":{6,7}," 5&#34 ;:{6,7}}," 2" {" 3":{6,7}," 4":{6,7},& #34; 5":{6,7}}}
任何帮助将不胜感激!
答案 0 :(得分:0)
var arr = [[1, 2], [ 3, 4]];
var obj = {};
function createMultiObject(arr) {
'use strict';
var i = 0;
for (i; i < arr[0].length; i = i + 1) {
obj[arr[0][i].toString()] = arr[1];
}
}
createMultiObject(arr);
console.log(obj);
&#13;
答案 1 :(得分:0)
我正在回答我自己的上述问题,因为如果有人需要,
function writeHtml(main, addthis) {
var arr = {};
if ($.isEmptyObject(main)) {
ar = addthis;
} else {
$.each(addthis, function(i, d) {
ar[d] = main;
});
}
return arr;
}
maxlen = uiarr.length - 1;
arr = {};
function callarr(i) {
if (i >= 0) {
arr = writeHtml(arr, uiarr[i]);
callarr(i - 1);
}
}
callarr(maxlen);
<强>,其中强>
uiarr = [
[1, 2],
[3, 4],
[5, 6, 7],
[8, 9, 10, 11]
];