One very common operation in programming is to iterate through an Array's contents, apply a function to each item, and create a new array containing the results. For example, let's say you wanted to loop through an array of stock objects and select only the name for display on screen. In this lesson we will demonstrate how to use the Array's map method to easily perform this operation with less code than a loop would require.

function getStockSymbols(stocks) {
return stocks.map(function(stock) {
return stock.symbol;
})
} var symbols = getStockSymbols([
{ symbol: "XFX", price: 240.22, volume: 23432 },
{ symbol: "TNZ", price: 332.19, volume: 234 },
{ symbol: "JXJ", price: 120.22, volume: 5323 },
]); console.log(JSON.stringify(symbols));

[Javascript] The Array map method的更多相关文章

  1. JavaScript 对象Array,Map,Set使用

    for(int i = 0 :i < 3 ;i++ ){ //[重点说三遍] 在说明每个对象的用法之前,首先说明 JavaScript 对象的使用一定要注意浏览器的兼容性问题!尤其是IE的版本! ...

  2. [Javascript] The Array forEach method

    Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...

  3. [Javascript] The Array filter method

    One very common operation in programming is to iterate through an Array's contents, apply a test fun ...

  4. JavaScript中数组map()方法

    JavaScript 数组map()方法创建一个新的数组使用调用此数组中的每个元素上所提供的函数的结果.语法 ? 1 array.map(callback[, thisObject]); 下面是参数的 ...

  5. Javascript中Array.prototype.map()详解

    map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...

  6. JavaScript Array.map

    Array.prototype.map() History Edit This article is in need of a technical review. Table of Contents ...

  7. [Javascript + rxjs] Using the map method with Observable

    Like an array, Observable has a map method that allows us to transform a sequence into a new Observa ...

  8. JavaScript Array map() 方法

    语法: array.map(function(currentValue,index,arr), thisValue) currentValue:必须.当前元素的值index:可选.当期元素的索引值ar ...

  9. javascript的Array.prototype.map()和jQuery的jQuery.map()

    两个方法都可以根据现有数组创建新数组,但在使用过程中发现有些不同之处 以下面这个数据为例: var numbers = [1, 3, 4, 6, 9]; 1. 对undefined和null的处理 a ...

随机推荐

  1. Percona Xtrabackup备份mysql全库及指定数据库(完整备份与增量备份)

    原文地址:http://www.tuicool.com/articles/RZRnq2 Xtrabackup简介 Percona XtraBackup是开源免费的MySQL数据库热备份软件,它能对In ...

  2. js中JSON对象和字符串对象相互转化

    JSON.stringify(value [, replacer] [, space]) //作用,将json数据转化为字符串value:是必须要的字段.就是你输入的对象,比如数组啊,类啊等等. re ...

  3. YIi 权限管理和基于角色的访问控制

    验证和授权(Authentication and Authorization) 定义身份类 (Defining Identity Class) 登录和注销(Login and Logout) 访问控制 ...

  4. Ember.js demo1

    <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1 ...

  5. Android-AttributeSet详解

    public interface AttributeSet { /** * Returns the number of attributes available in the set. * * @re ...

  6. ODBC具体使用

    应用程序 应用程序对外提供使用者交谈界面,同时对内执行资料之准备工作数据库系统所传回来的结果在显示给使用者看.简单来说,应用程序即ODBC 界面执行下列主要工作:1. Request a connec ...

  7. 【HDOJ】Power Stations

    DLX.针对每个城市,每个城市可充电的区间构成一个plan.每个决策由N*D个时间及N个精确覆盖构成. /* 3663 */ #include <iostream> #include &l ...

  8. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  9. 加快VisualStudio的开发速度--VS的一些开发技巧

    最近不得不使用VisualStudio来进行一些开发,用习惯了Eclipse,感觉VS很难上手,不过通过google,把VS进行Eclipse化,顺便记录下来,以防忘记. 1)显示文字的自动完成. 改 ...

  10. 致改变——总结&规划(2016·一)

    今天是立夏,过完这一天意味着农历2016年的第一季度已经过去了,也意味着真正的夏天已经来了.如果说春天是作物的播种期的话,那夏天可以看做是作物的成长期,也是农民伯伯们最繁忙的时期.本文主要对自己过去的 ...