0

Sorry about the title - I didn't know how else to explain it. However, I am initializing an object like this:

var form = {};
form.title = 'Titel';
form.fields = { type: 'radio', label: 'First name', id: 1 }

Then I'm adding to the object:

form.fields.choices = { text : "Standard", value : "Standard" , isSelected : false, price : ''};

That gives me the following array:

Array (
    [title] => Titel
    [fields] => Array
        (
            [type] => radio
            [label] => First name
            [id] => 1
            [choices] => Array
                (
                    [text] => Standard
                    [value] => Standard
                    [isSelected] => false
                    [price] => 
                )

        )

)

However, I would like to add multiple arrays to the choices key like this:

Array (
    [title] => Titel
    [fields] => Array
        (
            [type] => radio
            [label] => First name
            [id] => 1
            [choices] => Array(
                Array (
                    [text] => Standard1
                    [value] => Standard1
                    [isSelected] => false
                    [price] => ),
                Array (
                    [text] => Standard2
                    [value] => Standard2
                    [isSelected] => false
                    [price] => )

                )
            )

)

How would I achieve that?

3
  • You switch between object and arrays in your second code block. Do you want form.fields.choices to be an array or an object? Commented Aug 4, 2016 at 17:43
  • Note: they're not associative arrays, they're just objects. Commented Aug 4, 2016 at 17:43
  • Not really sure whether I need arrays or objects. I am sending it to PHP with Ajax, where I need it to have the following structure: pastebin.com/T83H5S8C Commented Aug 4, 2016 at 17:50

3 Answers 3

2

Make options an array

form.fields.choices = []

then

form.fields.choices[0] = { text : "Standard", value : "Standard" , isSelected : false, price : ''}

form.fields.choices[1] = { text : " Standard 2", value : "Standard" , isSelected : false, price : ''}
Sign up to request clarification or add additional context in comments.

4 Comments

Or use choices.push({...}) if you don't want to mess up with keys
Or in one line: form.fields.choices = [{ text : "Standard", value : "Standard" , isSelected : false, price : ''}, { text : " Standard 2", value : "Standard" , isSelected : false, price : ''}];
Hmm. I think the keys that I get by doing that is messing something up. I am sending the finished array to php with ajax. In my PHP script, it needs to imitate this structure: pastebin.com/T83H5S8C
that's a different question but make sure your doing the JSON serialization/deserialization correctly.
0

Something like this? Add increment values and another array accordingly.

    var choices = [];

    const choice = {
       <values>
    };

    Object.keys(choice).forEach((key, index) => {
        if (data.search(key) !== -1) {
            choices.push(choice[key]);
        }
    });

Comments

0
var form = [];

var form_object = {};
form_object.title = "Title";
form_object.fields = [];

var field_object = {};
field_object.type = "Radio";
field_object.label = "First name";
field_object.id = 1;
field_object.choices = [];

var choice_object1 = {};
choice_object1.text = "Standard";
choice_object1.value = "Standard";
choice_object1.isSelected = false;
choice_object1.price = '';

var choice_object2 = {};
choice_object2.text = "Standard 2";
choice_object2.value = "Standard 2";
choice_object2.isSelected = false;
choice_object2.price = '';

field_object.choices.push(choice_object1);
field_object.choices.push(choice_object2);

form_object.fields.push(field_object);

form.push(form_object);

Comments

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.