1

    function main() {
 
     let yesterdaysOrders = [
 
      {
        id: 1,
        orderLines: [{
            itemName: "Item 01",
            quantity: 1
          },
          {
            itemName: "Item 02",
            quantity: 3
          },
          {
            itemName: "Item 03",
            quantity: 25
          },
          {
            itemName: "Item 04",
            quantity: 12
          },
        ],
      },
      {
        id: 2,
        orderLines: [{
            itemName: "Item 01",
            quantity: 1
          },
          {
            itemName: "Item 08",
            quantity: 42
          },
          {
            itemName: "Item 09",
            quantity: 13
          },
          {
            itemName: "Item 12",
            quantity: 37
          },
        ],
      },
      {
        id: 3,
        orderLines: [{
          itemName: "Item 12",
          quantity: 16
        }, ],
      },
      {
        id: 4,
        orderLines: [{
            itemName: "Item 10",
            quantity: 11
          },
          {
            itemName: "Item 11",
            quantity: 10
          },
        ],
      },
      {
        id: 5,
        orderLines: [{
            itemName: "Item 06",
            quantity: 7
          },
          {
            itemName: "Item 07",
            quantity: 2
          },
          {
            itemName: "Item 12",
            quantity: 14
          },
        ],
      },
      {
        id: 6,
        orderLines: [{
          itemName: "Item 05",
          quantity: 17
        }, ],
      },
      {
        id: 7,
        orderLines: [{
            itemName: "Item 03",
            quantity: 5
          },
          {
            itemName: "Item 07",
            quantity: 2
          },
        ],
      },
      {
        id: 8,
        orderLines: [{
            itemName: "Item 02",
            quantity: 13
          },
          {
            itemName: "Item 07",
            quantity: 7
          },
          {
            itemName: "Item 09",
            quantity: 2
          },
        ],
      },
      {
        id: 9,
        orderLines: [{
            itemName: "Item 01",
            quantity: 4
          },
          {
            itemName: "Item 06",
            quantity: 17
          },
          {
            itemName: "Item 07",
            quantity: 3
          },
        ],
      },
      {
        id: 10,
        orderLines: [{
            itemName: "Item 11",
            quantity: 12
          },
          {
            itemName: "Item 12",
            quantity: 1
          },
        ],
      }
    ],
 
    result = Array.from(
      yesterdaysOrders.reduce((acc, {
        orderLines
      }) => {
        orderLines.forEach(({
          itemName,
          quantity
        }) => acc.set(itemName, (acc.get(itemName) || 0) + quantity));
        return acc;
      }, new Map), ([itemName, quantity]) => ({
        itemName,
        quantity
      }));
}

I have this function and at the end, I'm grabbing the itemName and quantity so that eventually I can display them in the DOM.

I'm looking to refactor the result = Array.from.... to be used with a spread operator, along with the already implemented destructuring.

How would I go about starting this off?

I'm understanding that the spread operator allows anything that is iterable to be able to be expanded.

Also, within the main function, after I place the comma and define my new function with result, what is the explanation for not needing a var, let, or const before it?

  • 1
    Why do you want to replace `Array.from` with a spread syntax literal? `Array.from` is a perfectly fine function to call for creating an *array from* a map. Also, you already are using destructuring everywhere you can - what do you want to change? – Bergi Sep 21 '19 at 19:03
  • @Bergi yes i agree, just want to see a different approach using `spread operator` and how that would have to then be arranged. and I clarified the statement regarding destructuring, –  Sep 21 '19 at 20:17
  • 1
    Well `Array.from(x)` is mostly equivalent to `[...x]`, but readability doesn't benefit. Also, in case the second argument of `Array.from` is used (like here), you cannot just use a spread syntax array literal, you would at least have to follow it up with `.map()`. – Bergi Sep 21 '19 at 20:23
  • 2
    Regarding the other question you edited in (please don't!), see [Declaring Multiple Variables in JavaScript](https://stackoverflow.com/q/694102/1048572). (Btw `result` is not declared as a function, it's a `let` variable getting initialised with an array) – Bergi Sep 21 '19 at 20:24
  • @Bergi ah gotcha, appreciate that, answers all my questions –  Sep 21 '19 at 20:26
  • @Bergi what are you referring to? `Regarding the other question you edited in (please don't)` –  Sep 22 '19 at 01:53
  • I was referring to [this edit](https://stackoverflow.com/posts/58042756/revisions#rev-arrow-f6a80d07-cc76-496f-b70a-32e1e4d08f4c) – Bergi Sep 22 '19 at 10:43
  • @Bergi oh gotcha, yeah i understand now, was confused by something but now im clear on it. Thanks. –  Sep 22 '19 at 18:41

0 Answers0