Guest User

Create an object with JavaScript

a guest
May 25th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let firstArr = ['category1', 'category2', 'category3', 'category4', 'category5']
  2. let secondArr = ['value1', 'value2', 'value3', 'value4', 'value5']
  3.  
  4. // I created a function to pass all the arrays in
  5. function populateDictionary(categoryList, key, dataArray, dictionary) {
  6.     dictionary[key] = {}
  7.     for (i=0; i<categoryList.length; i++) {
  8.         dictionary[key][categoryList[i]] = {name: categoryList[i], value: dataArray[i]}
  9.     }
  10. }
  11.  
  12. const dataObject = {}
  13.  
  14. populateDictionary(firstArr, 'expenses', secondArr, dataObject)
  15.  
  16. console.log(dataObject)
Advertisement
Add Comment
Please, Sign In to add comment