You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # collection-map [![NPM version](https://img.shields.io/npm/v/collection-map.svg?style=flat)](https://www.npmjs.com/package/collection-map) [![NPM monthly downloads](https://img.shields.io/npm/dm/collection-map.svg?style=flat)](https://npmjs.org/package/collection-map) [![NPM total downloads](https://img.shields.io/npm/dt/collection-map.svg?style=flat)](https://npmjs.org/package/collection-map) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/collection-map.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/collection-map)
  2. > Returns an array of mapped values from an array or object.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save collection-map
  7. ```
  8. Inspired by the [collections/map](http://moutjs.com/) util in mout.
  9. ## Usage
  10. ```js
  11. var map = require('collection-map');
  12. ```
  13. ### objects
  14. ```js
  15. var res = map({a: 'foo', b: 'bar', c: 'baz'}, function(item, key, obj) {
  16. return item;
  17. });
  18. console.log(res);
  19. // => ['foo', 'bar', 'baz']
  20. var res = map({a: 'foo', b: 'bar', c: 'baz'}, function(item, key, obj) {
  21. return key;
  22. });
  23. console.log(res);
  24. // => ['a', 'b', 'c']
  25. ```
  26. ### arrays
  27. ```js
  28. var res = map(['foo', 'bar', 'baz'], function(item, index, array) {
  29. return item;
  30. });
  31. console.log(res);
  32. // => ['foo', 'bar', 'baz']
  33. var res = map(['foo', 'bar', 'baz'], function(item, index, array) {
  34. return index;
  35. });
  36. console.log(res);
  37. // => [0, 1, 2]
  38. ```
  39. ### strings
  40. A string may be passed as the second argument, for getting properties:
  41. ```js
  42. var obj = {
  43. a: {aaa: 'one', bbb: 'four', ccc: 'seven'},
  44. b: {aaa: 'two', bbb: 'five', ccc: 'eight'},
  45. c: {aaa: 'three', bbb: 'six', ccc: 'nine'}
  46. };
  47. console.log(map(obj, 'aaa'));
  48. // => ['one', 'two', 'three']
  49. var array = [obj.a, obj.b, obj.c];
  50. console.log(map(array, 'bbb'));
  51. // => ['four', 'five', 'six']
  52. ```
  53. ### thisArg
  54. Invocation context may be passed as the last argument.
  55. ```js
  56. var array = ['a', 'b', 'c'];
  57. var ctx = {a: 'aaa', b: 'bbb', c: 'ccc'};
  58. var res = map(array, function(item, index, array) {
  59. return this[item];
  60. }, ctx);
  61. console.log(res);
  62. // => ['aaa', 'bbb', 'ccc']
  63. ```
  64. ## About
  65. ### Related projects
  66. * [arr-flatten](https://www.npmjs.com/package/arr-flatten): Recursively flatten an array or arrays. This is the fastest implementation of array flatten. | [homepage](https://github.com/jonschlinkert/arr-flatten "Recursively flatten an array or arrays. This is the fastest implementation of array flatten.")
  67. * [arr-map](https://www.npmjs.com/package/arr-map): Faster, node.js focused alternative to JavaScript's native array map. | [homepage](https://github.com/jonschlinkert/arr-map "Faster, node.js focused alternative to JavaScript's native array map.")
  68. * [for-in](https://www.npmjs.com/package/for-in): Iterate over the own and inherited enumerable properties of an object, and return an object… [more](https://github.com/jonschlinkert/for-in) | [homepage](https://github.com/jonschlinkert/for-in "Iterate over the own and inherited enumerable properties of an object, and return an object with properties that evaluate to true from the callback. Exit early by returning `false`. JavaScript/Node.js")
  69. * [for-own](https://www.npmjs.com/package/for-own): Iterate over the own enumerable properties of an object, and return an object with properties… [more](https://github.com/jonschlinkert/for-own) | [homepage](https://github.com/jonschlinkert/for-own "Iterate over the own enumerable properties of an object, and return an object with properties that evaluate to true from the callback. Exit early by returning `false`. JavaScript/Node.js.")
  70. ### Contributing
  71. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  72. ### Building docs
  73. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  74. To generate the readme, run the following command:
  75. ```sh
  76. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  77. ```
  78. ### Running tests
  79. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  80. ```sh
  81. $ npm install && npm test
  82. ```
  83. ### Author
  84. **Jon Schlinkert**
  85. * [github/jonschlinkert](https://github.com/jonschlinkert)
  86. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  87. ### License
  88. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  89. Released under the [MIT License](LICENSE).
  90. ***
  91. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.3, on March 02, 2017._