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

Map and filter to array (#27) #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rstacruz
Copy link
Contributor

See #27 :) This one does NOT update README.md with documentation... I had no idea how to do that. Nonetheless they should be well documented via jsdoc.

const mapToArray = require('object-loops/map-to-array')
const map = require('object-loops/map')

mapToArray({ a: 1, b: 2, c: 3 }, n => n * n)
//=> [ 1, 4, 9 ]

map({ a: 1, b: 2, c: 3 }, n => n * n)
//=> { a: 1, b: 4, c: 9 }

These functions will be helpful in dealing with React. Like this:

const ListPeople = () => {
  people = {
    john: "John Lennon",
    ringo: "Ringo Starr"
  }

  // A regular `map()` would break things here. It needs to return an array.
  return <div>
    {mapToArray(people, (name, id) => <li id={id}>Mr. ${name}</li>)}
  </div>
}

@rstacruz
Copy link
Contributor Author

It might be worth noting that this is the same behavior of lodash/underscore's _.map.

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.

1 participant