Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes incorrectly applied .forEach #2554

Merged
merged 1 commit into from Jul 28, 2022
Merged

Fixes incorrectly applied .forEach #2554

merged 1 commit into from Jul 28, 2022

Conversation

DilwoarH
Copy link
Contributor

.forEach was incorrectly made available to all DOM Iterators in core-js. This has now been fixed in the package: zloirock/core-js#988

The recommended approach for HTMLCollections objects is to use Array.from (https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection). This has been applied to the code to support the .forEach function.

This was originally noticed when updating the yarn.lock file in PR #2551.

Credits: @ollietreend for finding the solution.

⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

`.forEach` was incorrectly made available to all DOM Iterators in core-js. This has now been fixed in the package: zloirock/core-js#988

The recommended approach for HTMLCollections objects is to use `Array.from` (https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection). This has been applied to the code to support the `.forEach` function.

This was originally noticed when updating the yarn.lock file in PR #2551.

Credits: @ollietreend for finding the solution.
@@ -36,7 +36,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
id: $select.id,
source: function (query, syncResults) {
var results = []
$select.options.forEach(function ($el) {
Array.from($select.options).forEach(function ($el) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.from is a method that's not in IE. Do we get this polyfilled by core JS?

Could also use the boring old for (var i .. ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh! 🙈 Good spot @kevindew.

This should do the trick then:

var $el
for (var i = 0; i < $select.options.length; i++) {
  $el = $select.options[i]
  console.log($el)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2022-07-28 at 2 00 40 pm

Didn't have any contacts locally but Array.from() seems to work on IE11

Copy link
Contributor

@ollietreend ollietreend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@DilwoarH DilwoarH merged commit 488af61 into main Jul 28, 2022
@DilwoarH DilwoarH deleted the fix-foreach-bug branch July 28, 2022 13:55
@kevindew
Copy link
Member

Great stuff 👍

Always fun when we actually get to use other enumerators than the basic for loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants