How can I create an input dictionary with a forEach in Javascript?

2

What I want to do is create a dictionary in javascript and then use it in python. I have some dynamic inputs that have different attributes that I would like to put in a dictionary how can I do this?

I would like an output more or less if:

ditionary2 = {
    'key': { u'type': u'TYPES', u'value': u'value', u'type_value': u'STR', u'mask': bool},
    'key': { u'type': u'TYPES', u'value': u'value', u'type_value': u'TYPE_VALUES', u'mask': bool},
    'key': { u'type': u'TYPES', u'value': u'value', u'type_value': u'TYPE_VALUES', u'mask': bool},
    'key': { u'type': u'TYPES', u'value': u'value', u'type_value': u'TYPE_VALUES', u'mask': bool},
     }

My html is like this:

<div class="margin_bot" id="itemRows1">
  <p id="ProwNum1">
     <input class="params margin_bot input_text input_size text_input span-11" tipo="LAM" type="text" name="par_1" value="password" placeholder="Valor">
     <input class="params margin_bot input_text input_size text_input span-11" tipo="LAM" type="text" name="par_2" value="password2" placeholder="Valor"> 
  </p>
</div>

And my javacript function looks like this:

function create_kapps_dict(selector, content){
var items = $('p[id^="ProwNum"]').find('.params').filter(':visible');
var post_settings = [];
items.forEach( )
}

But for some reason it is not functional

    
asked by Rafael 23.08.2018 в 21:54
source

1 answer

1

You're confusing javascript with jQuery. You are using a jQuery selector to fill the variable items . If you are going to use jQuery you have to use .each() instead of forEach() . It would be something like this:

function create_kapps_dict(){
  var items = $('p[id^="ProwNum"]').find('.params').filter(':visible');
  var post_settings = [];
  items.each(function(i,item) {    
    var obj = {
      'type': $(item).attr('type'),
      'value': $(item).val(),
      'tipo': $(item).attr('tipo'),
      'mask': $(item).attr('placeholder')
    }
    post_settings.push(obj);
  });
  console.log(post_settings);
}

create_kapps_dict();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="margin_bot" id="itemRows1">
  <p id="ProwNum1">
     <input class="params margin_bot input_text input_size text_input span-11" tipo="LAM" type="text" name="par_1" value="password" placeholder="Valor">
     <input class="params margin_bot input_text input_size text_input span-11" tipo="LAM" type="text" name="par_2" value="password2" placeholder="Valor"> 
  </p>
</div>
    
answered by 23.08.2018 / 22:45
source