Search code examples
jqueryextend

jQuery extend with key in variable


I have this function to add key in a data with k="Toto"

 $Root = $("#" + n); $Root.data("TFO", $.extend({ k: v }, $Root.data("TFO")));

But when I see $Root.data("TFO") I get k instead of the value in k

How can I do it?


Solution

  • Try this:

     var obj = {};
     obj[ k ] = v; //<--------- VALUE of k will be used here & NOT k
     $Root = $("#" + n); 
     $Root.data("tfo", $.extend(obj, $Root.data("tfo")));
     //or $Root.data()['tfo'] = $.extend( obj, $Root.data('tfo') );