Chrome extension looping through javascript object not working -
i building chrome extension , running following script:
var group = { 'object1': { option1: '', option2: '' }, 'object2': { option1: '', option2: '' } }; chrome.tabs.executescript({ code: " \ var group_array = [" + object.keys(group) + "]; \ console.log(group_array[0]); \ " });
for reason, console.log
returns object1
not defined. wanted console.log
return name of first object in group
, should return object1
.
what doing wrong?
i test it. problem is:
"["+['object1','object2']+"]" -> "[object1,object2]"
so
var group_array = [object1, object2];
and if eval string directly, object1 , object2 treated variable instead of string, , since there not variable named object1 , object2, evaluation failed.
to achieve want, can loop array of object.keys(group), , append keys code 1 one, or can directly using json.stringify parse result string array:
" \ var group_array = " + json.stringify(object.keys(group)) + "; \ console.log(group_array[0]); \ "
Comments
Post a Comment