skip to Main Content

I have this code

    var sections = [];
    
    
    sections.add({'name': 'test', 'qustions' : []});
    sections[0]['qustions'] = {'asd':'qustions 1'};
    sections[0]['qustions'] = {'name':'qustions 2'};

    print(sections);

returns

[{name: test, qustions: {name: qustions 2}}]

I want it to returns something like

[{name: test, qustions: [{name: qustions 1},{name: qustions 2}]}]

2

Answers


  1. Every time you use map[key]=newValue, it reset the value for that key. what you need here is to perform add for list.

    var sections = [];
    
    sections.add({'name': 'test', 'qustions': []});
    
    sections[0]['qustions'].add({'asd': 'qustions 1'});
    sections[0]['qustions'].add({'name': 'qustions 2'});
    print(sections);
    

    You may also like to create model class.

    Login or Signup to reply.
  2. you can use insert method:

      var sections = [];
    
      sections.add({'name': 'test', 'qustions': []});
      sections[0]['qustions'] = {'name': 'qustions 1'};
      sections.insert(1, {'name': 'qustions 2'});
    

    output

    [{name: test, qustions: [{name: qustions 1},{name: qustions 2}]}]
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search