From 446b06d544bdbcc16cbde486eaaf4dd1f8afbf67 Mon Sep 17 00:00:00 2001 From: alexlee-dev Date: Wed, 21 Aug 2019 15:23:02 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Change=20some=20Array.forE?= =?UTF-8?q?ach()=20to=20use=20Array.filter()=20instead.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/redux/reducers/world.js | 6 +----- src/util.js | 9 ++++----- src/views/planets.js | 10 ++++------ 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/redux/reducers/world.js b/src/redux/reducers/world.js index a53fb2f..0b2f2f5 100644 --- a/src/redux/reducers/world.js +++ b/src/redux/reducers/world.js @@ -40,13 +40,9 @@ export default (state = worldDefaultState, action) => { const { isHomePlanet, items, location, name } = planet const planetContainsItem = items.includes(item) if (planetContainsItem) { - const itemIndex = items.findIndex(currentItem => item === currentItem) - const newItems = Array.from(items) - newItems.splice(itemIndex, 1) - const newPlanetObj = { isHomePlanet, - items: newItems, + items: items.filter(currentItem => item !== currentItem), location, name } diff --git a/src/util.js b/src/util.js index bc251eb..03d4776 100644 --- a/src/util.js +++ b/src/util.js @@ -42,11 +42,10 @@ export const generatePlanets = () => { planets.push({ isHomePlanet, location, name }) } - planets.forEach((planet, i) => { - const otherPlanets = [...planets] - otherPlanets.splice(i, 1) - - planet.items = generateItems(otherPlanets) + planets.forEach(planet => { + planet.items = generateItems( + planets.filter(currentPlanet => currentPlanet !== planet) + ) }) return planets diff --git a/src/views/planets.js b/src/views/planets.js index 6653596..2da42d4 100644 --- a/src/views/planets.js +++ b/src/views/planets.js @@ -116,12 +116,10 @@ const mapDispatchToProps = dispatch => ({ dispatch(removeItem(item)) }, handleShipTravel: (destination, shipCargo) => { - const sellableItems = [] - shipCargo.forEach(item => { - if (item.destination.value === destination.value) { - sellableItems.push(item) - } - }) + const sellableItems = shipCargo.filter( + item => item.destination.value === destination.value + ) + sellableItems.forEach(item => { // * Add the value of this item to the user's cash dispatch(addCash(item.value))