1

I have one array inside that I have some multiple array inside that I tried to arrange that array inside array but I dont know what is wrong ? so how to set array inside array properly? below arrayData is available.

const arrayData = [
  form_id: 1,
  countTotal: 100,
  formName: 'Application Form',
  questions: [
    0: [
      questionName: 'what is your name ?',
      note: 'give firstname middlename and lastname also..',
      required: true,
      answer: 'my name is John dao',
      sequence: 0,
      questionType: 'long text',
      options: [],
    ]
    1: [
      questionName: 'select your gender ?',
      note: '',
      required: true,
      answer: 'Male',
      sequence: 1,
      questionType: 'Single Selector',
      options: [
        0: {
          option_id: 1,
          value: 'Male',
          sequence: 0,
          is_selected: true,
        },
        1: {
          option_id: 2,
          value: 'female',
          sequence: 1,
          is_selected: false,
        },
        2: {
          option_id: 3,
          value: 'Other',
          sequence: 2,
          is_selected: false,
        },
      ],
    ]
  ]
]
2
  • Arrays can only have numeric keys. You are using named keys. Use {} instead of []. Commented Mar 1, 2019 at 11:45
  • if you use {}, it will return as object, not array. See: stackoverflow.com/questions/31299172/… Commented Mar 1, 2019 at 11:48

3 Answers 3

1

It should be like this:

const arrayData = [
     form_id : 1,
     countTotal: 100,
     formName: 'Application Form',
     questions: [
                 [
                   questionName => 'what is your name ?',
                   note => 'give firstname middlename and lastname also..',
                   required => true,
                   answer => 'my name is khan, but i am not terrorist',
                   sequence => 0,
                   questionType => 'long text',
                   options => [], 
                 ],
                 [
                   questionName => 'select your gender ?',
                   note => '',
                   required => true,
                   answer => 'Male',
                   sequence => 1,
                   questionType => 'Single Selector',
                   options => [
                       { option_id: 1,
                         value: 'Male',
                         sequence: 0,
                         is_selected: true,
                       },
                       { option_id: 2,
                         value: 'female',
                         sequence: 1,
                         is_selected: false,
                       },
                       { option_id: 3,
                         value: 'Other',
                         sequence: 2,
                         is_selected: false,
                       },
                   ], 
                 ]
                ]
   ]

You shouldn't give the index manually because It already registered automatically.

array = ['data index 0','data index 1','data index 2'];

Besides, you can register your own index by using this:

array = [index1 => 'data index 1', index2 => 'data index 2'];
Sign up to request clarification or add additional context in comments.

Comments

1

Though [] are used for array's but your array is not containing array items instead are more of dictionary item. So either make it dictionary using {} parenthesis or add items like this and it will make your array a list of dictionary

 [
     {form_id : 1},
     {countTotal: 100},
      ....
 ] 

and for array inside array , add like this

questions: [{  
                 questionName: 'what is your name ?',
                 note: 'give firstname middlename and lastname also..',
                 required: true,
                 answer: 'my name is John dao',
                 sequence: 0,
                 questionType: 'long text',
                 options: [], 
        },
        ....
        ]

Comments

0
  1. arrayData that you tried to make is not an array but an object. So contents should be in {} not in [].

Update: If it is really an array containing object like that, you need to put an object in the array.

[a: 1, b: 2]   // wrong
[{a: 1, b: 2}] // correct
  1. property like questions is type of array but but you are setting keys. You can't do it manually like that.
[0: 1, 1: 2] // wrong
[1, 2]       // correct
  1. and item of questions should be an object so its content should be in {} not in [].
[a: 1, b: 2] // wrong
{a: 1, b: 2} // correct

const arrayData = [{
  form_id: 1,
  countTotal: 100,
  formName: 'Application Form',
  questions: [{
      questionName: 'what is your name ?',
      note: 'give firstname middlename and lastname also..',
      required: true,
      answer: 'my name is khan, but i am not terrorist',
      sequence: 0,
      questionType: 'long text',
      options: [],
    },
    {
      questionName: 'select your gender ?',
      note: '',
      required: true,
      answer: 'Male',
      sequence: 1,
      questionType: 'Single Selector',
      options: [{
          option_id: 1,
          value: 'Male',
          sequence: 0,
          is_selected: true,
        },
        {
          option_id: 2,
          value: 'female',
          sequence: 1,
          is_selected: false,
        },
        {
          option_id: 3,
          value: 'Other',
          sequence: 2,
          is_selected: false,
        },
      ],
    }
  ]
}]

console.log(arrayData)

3 Comments

it is works but if I want arrayData as array so how to make arrayData as array ?
his code was right, you need to make an object instead of array
@Akki If it is really an array, you need to put an object in the array. I've updated my post.

Your Answer

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