-2

The following code works fine:

direction.route(
{
     start: '30 Baker St, Old City, YY 12345',
     end: '30 Bogus Address Ave, Fake, ZZ 12345',
     waypoints: [ "<address1>", "<address2>" ] //Array using literals
     
});

The problem I'm having is that I may have dozens of addresses stored in an array. The number of addresses in the array will vary.

The following code would work, but I need to be able to build the array within th JS object dynamically.

direction.route(
{
     start: '30 Baker St, Old City, YY 12345',
     end: '30 Bogus Address Ave, Fake, ZZ 12345',
     waypoints: [ address[0], address[1], address[2], ... ] //Array using variables
     
});
ezG
  • 391
  • 1
  • 17
  • 2
    Try using spread syntax, `waypoints: [ ...address ]` – Mr. Polywhirl Sep 15 '20 at 13:10
  • 3
    Does this answer your question? [Copy array by value](https://stackoverflow.com/questions/7486085/copy-array-by-value) – Heretic Monkey Sep 15 '20 at 13:12
  • 2
    You can't pass the array as is? `waypoints: address` – Guy Incognito Sep 15 '20 at 13:12
  • 2
    @GuyIncognito There are important caveats with that -- changes made to `waypoints` would affect `address`, for instance. – Heretic Monkey Sep 15 '20 at 13:14
  • 1
    So @HereticMonkey is right! The result will be like: `waypoints: address.slice()` – Anton Sep 15 '20 at 13:18
  • You don't say. That's why I asked, if the OP doesn't pass the original array because they actually need a copy, or do they just not know that the original array can be used. Cloning a large array if the function doesn't modify it would just waste memory. – Guy Incognito Sep 15 '20 at 13:20
  • @Mr.Polywhirl: That worked like a charm! Thank you! – ezG Sep 15 '20 at 13:23
  • @GuyIncognito Well, the OP, doesn't appear to know much about the issue in general, so I thought the warning would be good to have to forestall further duplicate questions later on about why `address` changes when they change `waypoints`... – Heretic Monkey Sep 15 '20 at 13:25
  • @GuyIncognito your `waypoints: address` also worked. However, I'm following your exchange with @HereticMonkey. I've got loads to learn here. Thanks – ezG Sep 15 '20 at 13:25
  • @HereticMonkey the link you gave me worked as well. `waypoints: address.slice()` – ezG Sep 15 '20 at 13:31
  • @ezG It should provide you with the opportunity to acknowledge that the linked question's answers answered your question as well. A button or something? It's been a while since I've gotten a look at the UX. If you click it, it will close this question, leaving it available as a signpost for others to find if they search for a question similar to yours. – Heretic Monkey Sep 15 '20 at 13:37

2 Answers2

1

Here is how I did it using the MapQuest sample addresses.

var address = [];
address.push('366 Columbus Ave, New York, NY 10024');
address.push('881 7th Ave, New York, NY 10019');

L.mapquest.directions().route({
  start: '350 5th Ave, New York, NY 10118',
  end: 'One Liberty Plaza, New York, NY 10006',
  waypoints: address OR waypoints: [address[0], address[1]]
});
MQBrian
  • 452
  • 1
  • 3
  • 7
0

Here is what I settled on

     var direction = L.mapquest.directions();
 direction.route(
 {
      start: '30 Baker St, Old City, YY 12345',
      end: '30 Bogus Address Ave, Fake, ZZ 12345',
      waypoints: locations2.slice(), //NOTE:  "Spread syntax" worked just as well here too.
      optimizeWaypoints: true
 });

Acknowledgments to the following for GIVING me the answer: Heretic-Monkey, Mr. PolyWhirl, and Guy Incognito

ezG
  • 391
  • 1
  • 17