0

With using the Chrome API, I am wondering how I would go about passing an array though the sync.set ability.

var itemname = ["itemid1", "itemid2", "itemid3"]
var itemvalue = ["itemval1", "itemval2", "itemval"]

for (i = 0; i < itemname.length; ++i)  {
  chrome.storage.sync.set({ itemname[i]: itemvalue [i] });
}

Information would be stored within the itemname and itemvalue which would be variables for their inner value. Now I know to a certain degree that this isn't exactly how this works, and if anyone has a possible closest working solution for storing many different pieces of information using one line rather than having multiple chrome.storage.sync.set tags cluttering the code, that would be amazing.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • Use ES6 syntax or an intermediate object: [Using a variable for a key in a JavaScript object literal](//stackoverflow.com/a/2274327) – wOxxOm May 17 '17 at 09:49

1 Answers1

0

The better way is to wrap your items into a separate object and sync it once:

var itemname = ["itemid1", "itemid2", "itemid3"]
var itemvalue = ["itemval1", "itemval2", "itemval"]

var obj = {};
for (i = 0; i < itemname.length; ++i)  {
  obj[itemname[i]] = itemvalue [i]
}
storage.sync.set({ items: obj });
Denis L
  • 3,209
  • 1
  • 25
  • 37