当前位置:网站首页>JS中 t, _ => 的解析
JS中 t, _ => 的解析
2022-04-23 05:52:00 【Front 小思】
data:
var t = [{
nodeCode: '59',
nodeName: '国际小学',
children: [{
nodeCode: '37',
nodeName: 'PYP小学',
children: [{
nodeCode: '99',
nodeName: '二年级',
children: null,
billOpened: null,
},
{
nodeCode: '100',
nodeName: '三年级',
children: null,
billOpened: true,
},
{
nodeCode: '101',
nodeName: '四年级',
children: null,
billOpened: true,
},
{
nodeCode: '102',
nodeName: '五年级',
children: [{
nodeCode: '1011',
nodeName: '一班',
children: null,
billOpened: true,
}, ],
billOpened: true,
},
],
billOpened: true,
}, ],
billOpened: true,
},
{
nodeCode: '59',
nodeName: '国际小学',
children: [{
nodeCode: '37',
nodeName: 'PYP小学',
children: [{
nodeCode: '99',
nodeName: '二年级',
children: null,
billOpened: null,
},
{
nodeCode: '100',
nodeName: '三年级',
children: null,
billOpened: true,
},
{
nodeCode: '101',
nodeName: '四年级',
children: null,
billOpened: true,
},
{
nodeCode: '102',
nodeName: '五年级',
children: [{
nodeCode: '1011',
nodeName: '一班',
children: null,
billOpened: true,
}, ],
billOpened: true,
},
],
billOpened: true,
}, ],
billOpened: true,
},
];
const recUpdate = (t, f) =>({
...t, children: t?.children?.map(c => recUpdate(c, f)), ...f(t) })
const formatBillOpened = t =>recUpdate(t, _ => ({
billOpened: false }))
formatBillOpened(t)
console.log(t)
recUpdate(t, _ => ({ billOpened: false })) 是箭头函数我很疑惑,这为什么要使用_作为函数参数呢?后来查了资料,才知道,正常情况下,我们如果写一个不需要参数的箭头函数,写法是这样的
recUpdate= () => {/代码/}
1
如果我们使用()写法,这个箭头函数将无法传递参数,如果以下划线作为参数
recUpdate(t, _ => ( {/代码/})
2
经过一番搜索,确认了这个是参考了golang 变量 下划线的思想
简单来说的话,就是这个recUpdate函数就可以有个参数,只是用 _ 来表示,但是内部执行的代码并不会调用参数,即对于参数没有兴趣。也可以理解为,这个变量是使用箭头函数的时候必须填写的,但是我并不想使用,也懒得起名字了,避免制造信息噪音。
()和 _ 这两种写法,实现的功能相同,不过我现在也更倾向于使用 _,因为_与()相比,占用像素点更少,更能有一种,无视此变量的意义!
全局修改t数组中billOpened为false
let loop = (t) => {
t.map(item => {
item.billOpened = false
item.children && loop(item.children)
})
}
loop(t)
console.log(t)
let strT=JSON.stringify(t).replace(/\s+/g,"");
let result=strT.replaceAll("\"billOpened\":true", "\"billOpened\":false")
console.log(JSON.parse(result))
版权声明
本文为[Front 小思]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45788691/article/details/124351527
边栏推荐
猜你喜欢
基于VGG对五种类别图片的迁移学习
PN结、二极管原理详解与应用
[UDS] unified diagnostic service (UDS)
Collection of practical tips for C language (continuously updated)
Makefile基础、常用函数及通用Makefile
Introduction to nonparametric camera distortion model
【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)
OpenCV使用 GenericIndex 进行 KNN 搜索
函数的调用过程
VHDL-任意分频器(50%占空比)
随机推荐
Round up a little detail of the round
搭建jpress个人博客
基于TensorFlow的线性回归实例
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms
C语言数组处理批量数据
[learn] HF net training
HDU-Tunnel Warfare
死区时间的分析与设置
C51/C52 特殊功能寄存器表
FOC SVPWM函数PWMC_SetPhaseVoltage解析
SSH 公钥 私钥的理解
【UDS统一诊断服务】四、诊断典型服务(1)— 诊断和通信管理功能单元
搭建openstack平台
[ThreadX] h743 + ThreadX + Filex migration record
【UDS统一诊断服务】三、应用层协议(2)
Call procedure of function
Quaternion multiplication
约瑟夫序列 线段树 O(nlogn)
Notes on advanced points of C language 2
基于VGG卷积神经网络的图像识别代码实现