keep objects in an array


How can I store objects in an array so that I can call a method for each object in a loop.

How I would like the result to be:

[[obj1, obj2],[obj3, obj4]].foreach((firstObj, secondObj) => {;

And the result will be as follows:

Iteration 1:;;

iteration 2:;;

Any advice is welcome

>Solution :

Use array destructuring in the argument list to "unpack" the pairs to 2 free variables:

[[obj1, obj2], [obj3, obj4]].forEach(([o1, o2]) => {;;

This is equivalent to doing (and indeed, this is what Babel compiles the above to)

  [obj1, obj2],
  [obj3, obj4]
].forEach((_ref) => {
  let [o1, o2] = _ref;;;

Leave a ReplyCancel reply