我目前正在用Javascript创建一个动态的多维数组。首先,我想创建一个动态的检查表数组。
首先,我创建了我的第一个核对表,然后我想为我的主核对表创建一个子核对表。然后,我还想为该特定的子核对清单创建一个子核对清单,依此类推。

请参阅我的以下脚本
function get_item_checklist(){ //should convert the sample image to object
var arr = [];
$("#checklist-body").find("div.card").each(function(){
var list_name = $(this).find("list_name").val();
var $body = $(this).find("div.card-body div.list_item");
if($body.length){
var list_arr = [];
var list_rec = [];
$(body).each(function(){
var desc = $(this).find("input.desc").val();
var list_no = parseInt($(this).find("input.req-no").val()) - 1;
var sub_no = $(this).find("input.sub-no").val();
list_rec.push({"list_no":list_no,"sub_no":sub_no});
if(list_no && !sub_no){
if(!list_arr[list_no]){
list_arr[list_no] = [];
}
list_arr[list_no].push({"desc":desc}); // will simply just push since there is no sub#
}else if(list_no && sub_no){
sub_no = parseInt(sub_no) - 1;
var parent_nos = look_parent_list_no(list_rec,sub_no);
if(parent_nos){
if(parent_nos.length == 1){ //for only one parent checklist
if(!list_arr[parent_nos[0]][sub.no]){ //if not set
list_arr[parent_nos[0]][sub.no] = [];
}
list_arr[parent_nos[0]][sub_no].push({"desc":desc});
}else{
// if parent nos are multiple
// END OF MY SCRIPT HERE <<<<<<--------
}
}
}
});
arr.push({"name":list_name,"description":list_arr});
}else{
arr.push({"name":list_name});
}
});
};
function look_parent_list_no(arr,no,arr1){ // this will get all the list # of parents in order to know where to put exactly the sub checklist
if(typeof arr1 != "object"){
arr1 = [];
}
for(key in arr){
console.log(arr[key],no);
if(arr[key].list_no == no && !arr[key].sub_no){
arr1.push(arr[key].list_no);
return arr1;
}else if(arr[key].list_no == no && arr[key].sub_no){
arr1.push(arr[key].list_no);
return look_parent_list_no(arr,arr[key].sub_no,arr1);
}
}
return false;
};我现在遇到了一个问题,如果一个子检查表的父项是parent,你可以在列表# 5中看到它。列表5应该在列表#4的内部,而列表#4在列表#2的内部。
下面的object是我对get_item_checklist()的预期输出
[
{
name: "My Parent list 1",
description :
[
{
desc: "sublist 1"
},
{
desc: "sublist 2",
items:
[
{
desc: "sublist 1 of 2"
},
{
desc: "sublist 2 of 2",
items: [
{
desc: "sublist 1 of 2 of 2"
}
]
}
]
}
]
}
]发布于 2019-06-04 21:23:21
下面的代码用于在给定的动态位置准确地插入代码,
let input = [
{ desc:"test1" , list_no : 0 ,sub_no:null },
{ desc:"test2" , list_no : 1 ,sub_no:null },
{ desc:"test3" , list_no : 2 ,sub_no:null },
{ desc:"test1 of 1" , list_no : 3 ,sub_no:0 },
{ desc:"test2 of 1" , list_no : 4 ,sub_no:0 },
{ desc:"test1 of 2" , list_no : 5 ,sub_no:1 },
{ desc:"test1 of 3" , list_no : 6 ,sub_no:2 },
{ desc:"test1 of test1 of test 3" , list_no : 7 ,sub_no:6 }
];
let op = [];
let trackMap = {};
input.forEach((obj) => {
let node = "list_no";
let sub_node = "sub_no";
let description = "desc";
if (obj[sub_node] === null) {
let objFormed = {desc : obj[description]};
trackMap[obj[node]] = objFormed;
op.push(objFormed);
}
else {
let objFormed = {desc : obj[description]};
let parent = trackMap[obj[sub_node]];
if (parent) {
parent.items || (parent.items = []);
parent.items.push(objFormed)
}
trackMap[obj[node]] = objFormed;
}
});
https://stackoverflow.com/questions/56444452
复制相似问题