当前位置:网站首页>ES6 map extract array object
ES6 map extract array object
2022-07-19 01:34:00 【Think of a name for a long time】
map Extract array objects
map You can quickly extract the same in the array object key;
See code snippet
var list= [{
name:' Zhang San ',age:16},{
name:' Zhao si ',age:18}];
var nameList = list.map(a=>a.name)//[' Zhang San ',' Zhao si ']
var ageList = list.map(a=>a.age)//[16,18]
Often used in scenes Pick up the price of all goods in the shopping cart 、 Extract the product pictures in the product list and so on
var list= [{
name:' Zhang San ',age:16},{
name:' Zhao si ',age:18}];
var nameList = [];
var ageList = [];
list.forEach(item=>{
if(item.name)
nameList.push(item.name)
if(item.age)
ageList.push(item.age)
})
nameList //[' Zhang San ',' Zhao si ']
ageList //[16,18]
use map Than forEach It can be more efficient and save time , Save code
By the way map and forEach What they have in common and what they don't have in common
The same thing
(1) They loop through every item in the array .
(2) Each execution of an anonymous function supports three parameters , The parameters are item( At present, every item ),index( Index value ),arr( Original array ).
(3) In anonymous functions this Is directed window.
(4) Only arrays can be traversed .
(5)forEach and map It's equivalent to a single-layer package for loop
Difference
(1)map() Allocates memory space to store the new array and returns ,forEach() No data will be returned .
(2)forEach() allow callback Change the elements of the original array .map() Return a new array .
边栏推荐
- The C Programming Language (2nd)--笔记--1.8
- C Programming Language (2nd Edition)--读书笔记--1.4
- 当 std::bind 遇上 this
- uni-app微信公众号(1)——网页授权登录
- Page layout - three column layout solution
- Vue project deployment, cleaning cache
- Day15 paging, filtering
- uni canvas截取图片
- 使用redis - zset做排行榜
- 今天的码农女孩学习了关于事件操作和ref属性的笔记并做了表单双向绑定的练习
猜你喜欢
![[GFCTF 2021]Baby_ Cve-2021-41773 on the web](/img/e0/7541860509b3b77f758447e377f2f7.png)
[GFCTF 2021]Baby_ Cve-2021-41773 on the web

使用leaflet仿原神提瓦特大地图制作日记

JSX syntax

Express的使用方法,路由的匹配与使用

Express项目创建以及其路由介绍
![Buuctf [bjdctf2020]ezphp1 detailed explanation](/img/10/d2593dff2f4a5f0ca41a9f651128df.png)
Buuctf [bjdctf2020]ezphp1 detailed explanation

JS get the suffix format of a file name

【ElenmentUI el-date-picker日期选择器,结束时间不得早于开始时间,且只能选择距开始时间指定天数的日期】

Uni app wechat official account (5) - add and modify addresses

cmake的基本使用
随机推荐
P6-day01-general attempt (P4 review)
Collection and summary of penetration test information
markdown编辑器语法——文字颜色、大小、字体与背景色的设置(转载)
Uni app wechat applet - Mall (4) - merchants
uni-app微信小程序——商城(8)——订单详情
(七)流程控制
今天的码农女孩做了关于呼吸灯的练习、受控组件和高阶组件的简答题
Uni app wechat applet - Mall (3) - Mall Homepage
MoveIt2——2.MoveIt在RViz中的快速入门
二分法查找
JS高阶函数 filter/map/reduce
TCP与UDP,TCP服务器与客户端,UDP服务器与客户端
(九)Shell 输入输出重定向
el-form特殊字符校验
C Programming Language (2nd Edition)--读书笔记--1.3
“我的”Bug大全(转载)
Record buuctf [netding Cup 2018] unfinish1 problem solving ideas
Summary of Applied Cryptography
computed和watch、watchEffect
Summary of XML external entity injection (xxE target recurrence)