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,
},
],
]
]
]
3 Answers 3
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'];
Comments
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
arrayDatathat 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
- property like
questionsis 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
- and item of
questionsshould 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)
{}instead of[].{}, it will return as object, not array. See: stackoverflow.com/questions/31299172/…