Miškyns
Miškyns

Reputation: 11

Parse text with multiple strings in brackets into arrays

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;
}

Upvotes: 1

Views: 71

Answers (1)

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

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]"]);

Upvotes: 1

Related Questions