3

I am using the Array.find method from this question I asked earlier and I have realized that it is not supported in IE. Is there a jQuery equivalent or even a different Javascript equivalent which is supported in IE?

var zones = [{
    "zone": "one",
    "zipcodes": ["69122", "69125", "69128", "69129"]
  },
  {
    "zone": "two",
    "zipcodes": ["67515", "67516", "67518", "67521"]
  }
];

$(function() {
  $('#userZip').submit(function(e) {
    e.preventDefault();
    var userZip = $('input[type="text"]').val();
    var zone = zones.find(function(zone) {
      return zone.zipcodes.indexOf(userZip) > -1;
    });
    alert("Zone: " + zone.zone);
  });
});
i {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="userZip">
  <i>Enter zip code "69122" as an example</i>
  <input type="text" placeholder="zip" />
  <input type="submit" />
</form>

2
  • 1
    Why not just patch it in legacy browsers? Relying on large libraries for simple compatibility patches doesn't seem like the best approach. Commented May 9, 2017 at 16:57
  • jQuery is already included in this project for other reasons so it wouldn't be adding anything. Commented May 9, 2017 at 17:01

5 Answers 5

1

A polyfill:

Array.prototype.find = Array.prototype.find || function (callback) {
    for (var i = 0; i < this.length; i++) {
      if (callback(this[i], i)) {
        return this[i];
      }
    }
};

If you want a better one, look under the polyfill section of the first link youve provided...

Sign up to request clarification or add additional context in comments.

Comments

1

IE9+ supports Array.filter if that's good enough?

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Obviously you'll potentially get multiple results so will need to just select the first, if that's your intention.

Comments

1

Or, you can also use a standard for loop?

var zones = [{
    "zone": "one",
    "zipcodes": ["69122", "69125", "69128", "69129"]
  },
  {
    "zone": "two",
    "zipcodes": ["67515", "67516", "67518", "67521"]
  }
];

function findZone(zns, zip) {
  var zone = {};
  for (var i = 0; i < zns.length; i++) {
    for (var j = 0; j < zns[i].zipcodes.length; j++) {
      if (zip === zns[i].zipcodes[j]) {
        zone = zns[i];
        break;
      }
    }
  }
  return zone;
}

console.log(findZone(zones, "67515"))

Comments

0

A function using jQuery:

$(function() {
  $('#userZip').submit(function(e) {
    e.preventDefault();
    var userZip = $('input[type="text"]').val();
    var zone = $.grep(zones, function (element, index) {
      if ($.inArray( userZip, element.zipcodes ) > -1) return true;
    });
    alert("Zone: " + zone[0].zone);
  });
});

fiddle here: https://jsfiddle.net/beekvang/p76f1zev/

Comments

0

jQuery.grep works similar to Array's find method in that it operates on Array-Like objects and filters using a provided function. The result is a new array with only those elements included that pass the test. It also has an optional invert argument.

e.g. for your code

var filteredZones = $.grep(zones, function(zone) {
  return zone.zipcodes.indexOf(userZip) > -1;
});

if (filteredZones.length) {
  var zone = filteredZones[0];
}

You can read more about it here - http://api.jquery.com/jquery.grep/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.