Skip to content

Commit

Permalink
🔀 Merge pull request #36 from alexlee-dev/feature/filter
Browse files Browse the repository at this point in the history
✏️ Change some Array.forEach() to use Array.filter() instead.
  • Loading branch information
Alex Lee committed Aug 21, 2019
2 parents 218ffc9 + 446b06d commit 9e4541e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
6 changes: 1 addition & 5 deletions src/redux/reducers/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions src/views/planets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 9e4541e

Please sign in to comment.