0

I am currently creating a dynamic multidimensional array in Javascript. First of all I want to create a dynamic checklist array.

First I create my first checklist then I want to make a sub checklist for my main checklist. Then I want to create a sub checklist for that particular sub checklist also and so on.

enter image description here

see my below script

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;
};

I am now having a problem if a sub checklist have a parent of parent, you can see it in the list # 5. List 5 should be inside of list #4 where in list #4 is inside of list #2.

below object is my expected output to my 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"
              }
            ]
          }
        ]
      }
    ]
  }
]
8
  • This looks like a case which may fall under the Composite Design Pattern, you may want to look at this: (dofactory.com/javascript/composite-design-pattern) Commented Jun 4, 2019 at 13:02
  • Is it possible to reduce the amount of code you're showing here? It's a little difficult to focus on the specific problem here Commented Jun 4, 2019 at 13:04
  • just focus on my last script with example. if I have a dynamic keys how can I set the array using those keys Commented Jun 4, 2019 at 13:18
  • @OliverRadini Why did you delete your answer? and down voted my question? Just because i am in favor of other answer? I just chose which is the best answer. you can justify your answer if you think it should be chosen over the other Commented Jun 6, 2019 at 8:52
  • @JohnChristianDeChavez because the answer I gave didn't properly answer the question and I don't have time to correct it. I downvoted your question because it doesn't clearly state what is required. You could improve the question by providing a clear example of the data you have, and what you need to do with it, without any extra details. That'll make it easier for people to give good answers Commented Jun 6, 2019 at 9:01

1 Answer 1

1

The below code is used to insert the code exactly in the given dynamic position,

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; 
} 
});

Sign up to request clarification or add additional context in comments.

7 Comments

Hello, you code seems can run only once. if tried to run again it will get an error
Hi, Can you post what kind of error you are getting?
im getting undefined property.
Whenever the program runs based on the given dynamic keys it will traverse through the length of that array and each position is taken and multidimensional array is formed. The positions which are not given in the dynamic keys array will remain as holes. For me every time it gives me the result and not throwing error.
I tried recoding already your answer and i got it working.Now i have a big problem because of this multidimension array for my dynamic checklist. I updated my answer, you can see to the last my expected output.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.