Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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) => {
   firstobj.search();
   secondObj.search()
})

And the result will be as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Iteration 1: obj1.search(); obj2.search();

iteration 2: obj3.search(); obj4.search();

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]) => {
   o1.search();
   o2.search();
});

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;
  o1.search();
  o2.search();
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading