当前位置:网站首页>Have you ever used the comma operator?
Have you ever used the comma operator?
2022-07-27 16:06:00 【Moving maple】
What is the comma operator ?mdn explain : Evaluate each of its operands ( From left to right ), And return the value of the last operand . translate : Such as (x,y), Calculate from left to right x and y, Finally back to y
mdn Case study :
let x = 1;
x = (x++, x);
console.log(x); // 2
Does it feel like this comma operator doesn't work ? Existence makes sense , It is still useful in some scenes .
Take a chestnut :( use reduce Can achieve reverse)
var arr = [1,2,3,4,5]
var result = arr.reduce((t,v)=> (t.unshift(v),t),[])
console.log(result) // [5,4,3,2,1]
reduce What is it again? ?mdn explain :reduce() Method to execute a reducer function ( Ascending execution ), Summarize its results into a single return value . I'd better translate it : ok , Unable to translate , Anyway, it's the function of accumulator
reduce(func,initValue)
Parameters :
func: Callback function , And there are four parameters acc: accumulator ( To put it bluntly, you can get the last return value )cur: Current value idx: Current index oarr: Original array
initValue: Initial value
Let's take a look at the chestnut just now :
var arr = [1,2,3,4,5]
var result = arr.reduce((t,v)=> (t.unshift(v),t),[])
console.log(result) // [5,4,3,2,1]
I may ask if it's good to do this directly :var result = arr.reduce((t,v)=> t.unshift(v),[])
Come on, read the newspaper wrong :Uncaught TypeError: t.unshift is not a function
unshift Method returns the length of the array , So next time t Namely number Type instead of array , So this error will be reported .
Take apart this writing method is :
var result = arr.reduce((t,v)=> {
t.unshift(v);
return t;
},[])
Then the operation of the comma operator can simplify the writing :
var result = arr.reduce((t,v)=> (t.unshift(v),t),[])
边栏推荐
- [sword finger offer] interview question 45: arrange the array into the smallest number
- Constraints, design and joint query of data table -- 8000 word strategy + Exercise answers
- 线程间等待与唤醒机制、单例模式、阻塞队列、定时器
- openwrt 增加RTC(MCP7940 I2C总线)驱动详解
- Is the array name the address of the first element?
- To meet risc-v challenges? ARM CPU introduces custom instruction function!
- Solve mt7620 continuous cycle uboot (LZMA error 1 - must reset board to recover)
- 初识MySQL数据库
- 网络设备硬核技术内幕 路由器篇 22
- [sword finger offer] interview question 46: translating numbers into strings - dynamic programming
猜你喜欢

Flask连接mysql数据库已有表

keil 采用 makefile 实现编译

Openwrt adds RTC (mcp7940 I2C bus) drive details

leetcode25题:K 个一组翻转链表——链表困难题目详解

leetcode234题-简单方法判断回文链表
![[sword finger offer] interview question 53-i: find the number 1 in the sorted array -- three templates for binary search](/img/4b/460ac517e9a5d840a0961f5d7d8c9d.png)
[sword finger offer] interview question 53-i: find the number 1 in the sorted array -- three templates for binary search

渗透测试-干货 | 80篇+网络安全面试经验帖(面试篇)

Solve mt7620 continuous cycle uboot (LZMA error 1 - must reset board to recover)

C language: Sanzi game

线程间等待与唤醒机制、单例模式、阻塞队列、定时器
随机推荐
通俗易懂地区分++i和i++
多行文本溢出打点
少见的按位操作符
初识MySQL数据库
Flask连接mysql数据库已有表
leetcode234题-简单方法判断回文链表
网络设备硬核技术内幕 路由器篇 22
网络设备硬核技术内幕 路由器篇 小结(下)
[sword finger offer] interview question 50: the first character that appears only once - hash table lookup
Network principle (2) -- network development
Addition, deletion, query and modification of MySQL table data
profileapi.h header
Insert sort directly
Keil implements compilation with makefile
Division of entity classes (VO, do, dto)
Mlx90640 infrared thermal imager temperature sensor module development notes (VII)
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
C language: minesweeping games
CAS比较交换的知识、ABA问题、锁升级的流程
剑指 Offer 51. 数组中的逆序对