js中数组的map()方法
map()方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值
map()方法按照原是数组顺序以此处理元素
注意:map()不会对空数组进行检测 ;不会改变原始的数组

实例:
var numbers = [65, 44, 12, 4];
function multiplyArrayElement(num) {
return num * 10;
}
function myFunction() {
document.getElementById("demo").innerHTML = numbers.map(multiplyArrayElement);
}
console的结果为:650, 440, 120, 40
扩充:Object.keys()方法的使用
Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for...in 循环遍历该对象时返回的顺序一致


此外:
Object.getOwnPropertyNames()和Object.create()有待研究。 综合实例:
var obj={
errMsg: "getImageInfo:ok",
width: 750,
height: 571,
type: "png",
orientation: "up",
path: "http://tmp/touristappid.o6zAJs5BccurxyYtUj_ooLuxh5sg.LybVb8l5vngT297ff4e4aea3acaa3ec94eba8c6de637.png"
}
function format(obj) {
return '{\n' +
Object.keys(obj).map(function(key) {
return ' ' + key + ': ' + obj[key] + ','
}).join('\n') + '\n' +
'}'
}
调用format之后 console结果:{
errMsg: getImageInfo:ok,
width: 750,
height: 571,
type: png,
orientation: up,
path: http://tmp/touristappid.o6zAJs5BccurxyYtUj_ooLuxh5sg.LybVb8l5vngT297ff4e4aea3acaa3ec94eba8c6de637.png,
}
参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
js中数组的map()方法的更多相关文章
- 【前端_js】javascript中数组的map()方法
数组的map()方法用于遍历数组,每遍历一个元素就调用回调方法一次,并将回调函数的返回结果作为新数组的元素,被遍历的数组不会被改变. 语法:let newAarray = arr.map(functi ...
- javascript中数组的map方法
map方法原型:array1.map(callbackfn[, thisArg]) 参数: array1,必选. 一个数组对象.该函数一般用于数组对象 callbackfn,必选. 最多可以接受三个参 ...
- JS中数组的迭代方法和归并方法
昨天总结的JavaScript中的数组Array方法 数组的迭代方法 ES5中为数组定义了5个迭代方法.每个方法都要接收两个参数:要在每一项上面运行的函数和(可选的)运行该函数的作用域对象---影响t ...
- JS中数组和字符串方法的简单整理
一.数组: 数组的基本方法: 1.增:arr.unshift() /push() 前增/后增 2.删:arr.shift() /pop ...
- js中数组的检测方法
在js中可以使用Object.prototype.toString.call()的来检测一个对象是否为一个数组 //检测数组 var a = [1, 2]; console.log(typeof a) ...
- JS中数组的拷贝方法
之前在写一个vue的计算属性时,大概是这样: computed: { updateList () { let newList = this.List /*do something*/ return n ...
- js中的forEach/map方法
// forEach方法 var data = [{ name: "程咬金", sex: "1", age: 26 }, { name: "程才&qu ...
- JS中数组重排序方法
在数组中有两个可以用来直接排序的方法,分别是reverse()和sort().下面通过本文给大家详细介绍,对js数组重排序相关知识感兴趣的朋友一起看看吧 1.数组中已存在两个可直接用来重排序的方法:r ...
- js中数组去重的方法
在实际工作或面试中,我们经常会遇到"数组去重"问题,接下来就是使用js实现的数组去重的多种方法: 1.借助ES6提供的Set结构 var arr = [1,1,2,2,3,3,4, ...
随机推荐
- JS加密对应的c#解码
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,? ...
- LVM学习笔记
LVM Logical Volume Manager Volume management creates a layer of abstraction over physical storage, a ...
- [转]Setting Keystone v3 domains
http://www.florentflament.com/blog/setting-keystone-v3-domains.html The Openstack Identity v3 API, p ...
- 第三次作业(ABC类代码优化及感悟)
这次作业与林杰同学合作完成,不重复发布了. 可以直接去林杰同学的博客看.http://www.cnblogs.com/linlinlin/p/4836707.html
- freekan5.9电影网站安装及源码分享
Freekan是一套目前非常火的电影网站系统,全自动采集,支持对接公众号 服务器环境:centos7,宝塔面板,php7.1(重要),nignx,mysql 1.首先上传压缩包到网站目录,然后解压 2 ...
- [Swift]LeetCode168. Excel表列名称 | Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- [Swift]LeetCode280. 摆动排序 $ Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [Swift]LeetCode667. 优美的排列 II | Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- linux中的shell脚本编程---初识shell
Shell是用户与Linux或Unix内核通信的工具,shell编程指的并不是编写这个工具,而是指利用现有的shell工具进行编程,写出来的程序是轻量级的脚本,我们叫做shell脚本. Shell的语 ...
- asp.net mvc学习(Vs技巧与Httpcontext)
模型绑定分析 博客模拟的表单已经可以包含网站开发过程中遇到的大部分的表单格式了,包含一些数组.对象等等. 1.直接拼接字符串 $.ajax({ url: "/XXX", type: ...