0

so I looked at multiple sources including Finding All Combinations of JavaScript array values and https://codereview.stackexchange.com/questions/52119/calculate-all-possible-combinations-of-an-array-of-arrays-or-strings and am still coming up with a blank.

I am splitting the string "35 w 35 ave" into an array on " " which I then compare to an arrays for values. Here is the object:

var addressObject = {
    ave : ["av", "aven", "avenu", "avenue", "avn", "avnue"],
    w : ["west", "wst", "w"]
};

I add this to var address = txtNode.val().split(' '); which returns the array ['35','w','35','ave'] and then I take the array and put it through a nested forEach loop like so:

address.forEach( function(addressElement, indexOfAddressElement) {
    var abbreviationMatch = addressObject.hasOwnProperty(addressElement);
    if (abbreviationMatch) { 
        function addSentenceVariationsToAddressLists(arrayToLookAt) {
            arrayToLookAt.forEach(function (anItem, a) {
                var y = address.splice(indexOfAddressElement, 1, anItem); 
                addressLists.push(address.join(" ")); 
            })
        }
        var elementsFromArray = addressObject[addressElement];
        addSentenceVariationsToAddressLists(elementsFromArray);
    }    
});
console.log(addressLists);

This returns ["35 west 35 ave", "35 wst 35 ave", "35 w 35 ave", "35 w 35 av", "35 w 35 aven", "35 w 35 avenu", "35 w 35 avenue", "35 w 35 avn", "35 w 35 avnue"]

My question is: how do I return all of the variations of "ave" with "wst" and "west" instead of just "w" including the original value of the address array?

Community
  • 1
  • 1
  • Why do you have `"w"` in your `addressObject`'s `w` property in the first place. – Redu Sep 27 '16 at 22:40
  • Because when I take it out it doesn't show up in the addressLists array, it just shows `["35 west 35 ave", "35 wst 35 ave", "35 wst 35 av", "35 wst 35 aven", "35 wst 35 avenu", "35 wst 35 avenue", "35 wst 35 avn", "35 wst 35 avnue"]` – That One Dude Mike Sep 27 '16 at 23:06

1 Answers1

0

Okay, so this took some time to figure out, because I was trying to tweak your code instead of completely refactoring. Unfortunately, I think this code was flawed from an underlying structure aspect, so I ended up having to change quite a bit to get it working as intended with optimal reuse capabilities.

Keep in mind that this code does not account for other street variations (street, place, drive, etc.) nor other directions (East, North, South). It also does not account for double street names. For example, "25 w west ave" will replace "west" with the variations, and not the w. This would be an issue if/when you add the other street/direction variations, because there are streets out there like "123 west avenue street" or "123 north west avenue", etc.

Let me know how this works for you, and if you have any questions!

var addressObject = {
  streets: ["av", "aven", "avenu", "avenue", "avn", "avnue", "ave"],
  directions: ["west", "wst", "w"]
};
var adr = "35 w 35 ave";

function addSentenceVariationsToAddressLists(address, indexes) {
  var addresses = [];
  addressObject.streets.forEach(function(street) {
    addressObject.directions.forEach(function(direction) {
      var y = address.slice();
      y[indexes.streetIndex] = street;
      y[indexes.directionIndex] = direction;
      addresses.push(y.join(" "));
    });
  });
  return addresses;
}

function getAddressLists(address) {
  address = address.split(" "); // ['35', 'w', '35', 'ave']
  var indexes = {
    streetIndex: -1,
    directionIndex: -1
  };
  address.forEach(function(addressElement, indexOfAddressElement) {
    if (addressObject.streets.indexOf(addressElement) > -1) {
      indexes.streetIndex = indexOfAddressElement;
    } else if (addressObject.directions.indexOf(addressElement) > -1) {
      indexes.directionIndex = indexOfAddressElement;
    }
  });
  return addSentenceVariationsToAddressLists(address, indexes);
}
var addressLists = getAddressLists(adr);
console.log(addressLists);
mhodges
  • 10,938
  • 2
  • 28
  • 46