From 985ce1cb8bcb3f78561864e9f922d7f7cd0e96ca Mon Sep 17 00:00:00 2001 From: Eric Kwoka <43540491+ekwoka@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:33:17 +0400 Subject: [PATCH] :recycle: Uses Map --- packages/alpinejs/src/directives/x-for.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/alpinejs/src/directives/x-for.js b/packages/alpinejs/src/directives/x-for.js index a91c3f930..a236de083 100644 --- a/packages/alpinejs/src/directives/x-for.js +++ b/packages/alpinejs/src/directives/x-for.js @@ -62,9 +62,9 @@ function loop(templateEl, iteratorNames, evaluateItems, evaluateKey) { const oldLookup = templateEl._x_lookup const lookup = new Map() templateEl._x_lookup = lookup - const scopeEntries = [] + const hasStringKeys = isObject(items) - Object.entries(items).forEach(([index, item]) => { + const scopeEntries = Object.entries(items).map(([index, item]) => { if (!hasStringKeys) index = parseInt(index) const scope = getIterationScopeVariables( iteratorNames, @@ -72,25 +72,26 @@ function loop(templateEl, iteratorNames, evaluateItems, evaluateKey) { index, items ) - + let key; evaluateKey( - key => { - if (typeof key === 'object') + innerKey => { + if (typeof innerKey === 'object') warn( 'x-for key cannot be an object, it must be a string or an integer', templateEl ) - if (oldLookup.has(key)) { - lookup.set(key, oldLookup.get(key)) - oldLookup.delete(key) + if (oldLookup.has(innerKey)) { + lookup.set(innerKey, oldLookup.get(innerKey)) + oldLookup.delete(innerKey) } - scopeEntries.push([key, scope]) + key = innerKey }, { scope: { index, ...scope }, } ) + return [key, scope]; }) mutateDom(() => {