0

There are some posts about this subject already, but this use case is a bit different in my point of view. I'd like to change my dictionary item key depending on the function call, so for the code below, we need to focus on field parameter only.

As you can see field parameter contains 'font_size' string, I'd like to be able to alter t.fields.variable to t.fields.font_size depending on the field variable's name.

Javascript :

function obj_stringify(type, field, name, value) {

  data.forEach(function(d, index){
    var mindex = index;
    d.forEach(function(t) {
      if (t.type == type) {
         if (t.infos == name) {
            t.fields.field = value; /* would become : t.fields.font_size = value; */
         }
      }
    });
  });

  $('#json_dt_r').val(JSON.stringify(data[0], undefined, 4));
  $('#json_dt_v').val(JSON.stringify(data[1], undefined, 4));
}

Call :

obj_stringify('text', 'font_size', element_name, fontsize); 

I wasn't sure if it was possible to alter key with function arguments, is there a way?

cezar
  • 11,616
  • 6
  • 48
  • 84
Horai Nuri
  • 5,358
  • 16
  • 75
  • 127

0 Answers0