1

I have a problem with parsing something like this:

{form_settings['settings']['title']: "Titulek", form_settings['settings']['description']: "Popisek formuláře...", form_settings['settings']['gdpr']: "ip", form_settings['settings']['acquisition']: "n", form_settings['settings']['style_form']: "without_border", …}

I want something like in the PHP, where you can call this

$form_settings['settings']['title']

But you can't do it in javascript and i want to use similiar array like in the php I want to do something like that

form_settings['settings']['title']

Because it doesn't work, do you know how to parse it? Thanks for the answers!

//EDIT

<div class="col-md-10 col-form-label">
  <input type="text" id="title-for-receiver" class="form-control" name="form_settings['email']['{$input_form['EMAIL']['title_for_receiver']->name}']" value={$input_form['EMAIL']['title_for_receiver']->value} />
</div>

And I got these values through javascript

function getFormData(form){
var unindexed_array = form.serializeArray();
var indexed_array = {};

$.map(unindexed_array, function(n, i){
    indexed_array[n['name']] = n['value'];
});

return indexed_array;
}
12
  • Are you asking about parsing strings or targeting nested arrays? That doesn't look like valid JavaScript syntax, so I'm not sure what you're after. Commented Nov 21, 2018 at 20:55
  • 1
    I mean, that's not valid JSON. Why not make it valid JSON? Commented Nov 21, 2018 at 20:57
  • Yeah I want to parse it and save those strings in brackets to the arrays, which are named like the first string in bracket. So if you do something like tihs: form_settings['settings']['title'] you will get a Titulek string. Commented Nov 21, 2018 at 20:58
  • I've got them from html, because I have a name="form_settings['settings']['title']" and I got it by javascript I have more of these names on the same page, but there are different second bracket names. And I want to do it like in PHP. Commented Nov 21, 2018 at 21:00
  • If you do form_settings['settings']['title'] in JavaScript you'll get only the title string. (I had to look up "titulek".) Commented Nov 21, 2018 at 21:00

1 Answer 1

1

This is not something you can parse. That "parse" indicates a string or something that can be. THIS: cannot be "parsed":

  1. It has both single ' and double " quotes so NEITHER will work to make it a string which is a parse-able object.
  2. It has internal elements with no quotes also

{form_settings['settings']['title']: "Titulek", form_settings['settings']['description']: "Popisek formuláře...", form_settings['settings']['gdpr']: "ip", form_settings['settings']['acquisition']: "n", form_settings['settings']['style_form']: "without_border", …}

EDIT: I added mythingString and the parse of that to be more patently obvious.

Perhaps pass a better object:

var mything = {
  "form_settings['settings']['title']": "Titulek",
  "form_settings['settings']['description']": "Popisek formuláře...",
  "form_settings['settings']['gdpr']": "ip",
  "form_settings['settings']['acquisition']": "n",
  "form_settings['settings']['style_form']": "without_border"
};

console.log(mything["form_settings['settings']['title']"]);

var mything2 = {
  form_settings: {
    settings: {
      'title': "Titulek",
      'description': "Popisek formuláře...",
      'gdpr': "ip",
      'acquisition': "n",
      'style_form': "without_border"
    }
  }
};
console.log(mything2.form_settings['settings']['title']);
console.log(mything2.form_settings.settings.title);

var mythingString = '{"form_settings[settings][title]": "Titulek",  "form_settings[settings][description]": "Popisek formuláře...", "form_settings[settings][gdpr]": "ip",  "form_settings[settings][acquisition]": "n", "form_settings[settings][style_form]": "without_border"}';

var parsedThing = JSON.parse(mythingString);
console.log(parsedThing["form_settings[settings][title]"]);

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

2 Comments

Yes that is the thing I want to do from the first "mything" to "mything2", I want to get it from HTML "name" and inside the elements mything2.form_settings['settings']['title'] would be value with title. How can I do that?
@Miškyns So, first get mything you do not have that. That is the point.

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.