> For the complete documentation index, see [llms.txt](https://lodash.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lodash.shujuwajue.com/collection/map.md).

# \_.map（迭代遍历处理数组或对象元素）

**语法：**

```javascript
_.map(collection, [iteratee=_.identity])
```

**源代码链接：**[source](https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L9552)

**npm包链接：**[npm package](https://www.npmjs.com/package/lodash.map)

**描述：**

创建一个数组， value（值） 是`iteratee`（迭代函数）遍历`collection`（集合）中的每个元素后返回的结果。 iteratee（迭代函数）调用3个参数：*(value, index|key, collection)*.

lodash 中有许多方法是防止作为其他方法的迭代函数，例如：[`_.every`](https://lodash.com/docs/4.17.10#every),[`_.filter`](https://lodash.com/docs/4.17.10#filter),[`_.map`](https://lodash.com/docs/4.17.10#map),[`_.mapValues`](https://lodash.com/docs/4.17.10#mapValues),[`_.reject`](https://lodash.com/docs/4.17.10#reject), 和[`_.some`](https://lodash.com/docs/4.17.10#some).

受保护的方法有（注：即这些方法不能使用[`_.every`](https://lodash.com/docs/4.17.10#every),[`_.filter`](https://lodash.com/docs/4.17.10#filter),[`_.map`](https://lodash.com/docs/4.17.10#map),[`_.mapValues`](https://lodash.com/docs/4.17.10#mapValues),[`_.reject`](https://lodash.com/docs/4.17.10#reject), 和[`_.some`](https://lodash.com/docs/4.17.10#some)作为 iteratee 迭代函数参数）：ary, chunk, curry, curryRight, drop, dropRight, every, fill, invert, parseInt, random, range, rangeRight, repeat, sampleSize, slice, some, sortBy, split, take, takeRight, template, trim, trimEnd, trimStart, 和 words

**开始版本：**&#x30;.1.0

**参数：**

* `collection (Array|Object)`: 用来迭代的集合。
* `[iteratee=_.identity] (Array|Function|Object|string)`: 每次迭代调用的函数。

**返回值：**

* `(Array)`: 返回新的映射后数组。

**例子：**

```javascript
function square(n) {
  return n * n;
}

_.map([4, 8], square);
// => [16, 64]

_.map({ 'a': 4, 'b': 8 }, square);
// => [16, 64] (iteration order is not guaranteed)

var users = [
  { 'user': 'barney' },
  { 'user': 'fred' }
];

// The `_.property` iteratee shorthand.
_.map(users, 'user');
// => ['barney', 'fred']
```
