0

I need your little help to create the obj object you see on the code below.

// let arr1 = ['Monday', ['Pasta', 'Watermelon', 'Milk']],
// let arr2 = ['Tuesday', ['Cookies', 'Chips', 'Peach']]

const obj = {
  "Monday": ['Pasta', 'Watermelon', 'Milk'],
  "Tuesday": ['Cookies', 'Chips', 'Peach']
};

for (const [key, value] of Object.entries(obj)) {
  console.log(`${key} -> ${value}`);
}

Given arr1 and arr2 I have to create the obj object shown.

Can anyone help me please?

Sarah
  • 292
  • 1
  • 10
  • 1
    `obj[arr1[0]] = arr1[1]`. And the same for `arr2`. – Barmar Jul 06 '23 at 21:57
  • 1
    `Object.fromEntries([arr1, arr2])` – Andrew Parks Jul 06 '23 at 21:59
  • For reference: Barmar's comment is using [computed property names](https://eloquentcode.com/computed-property-names-in-javascript) for the keys; you can create a key from each array's first element, and then assign it the second array as its value. – Andy Jul 06 '23 at 22:04
  • 1
    @James not necessary. fromEntries needs an array of key-value pair arrays. Each of the source arrays is a single key-value pair array. – Andrew Parks Jul 06 '23 at 22:06

0 Answers0