当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Assembly base code example
[UDS unified diagnostic service] II. Network layer protocol (1) - overview and functions of network layer
圆整 round 的一点点小细节
Running QT program in visual Stdio
token详解以及应用原理
Wechat applet request encapsulation
MOS管特性和导通过程
QT add qserialport class to realize serial port operation
基于Keras的时装分类案例
_findnext 报错
Sdoi2009-hh Necklace
Shell脚本的通配符和特殊符号
POJ-The Unique MST
Shell脚本 &&和||的使用
C语言实现memcpy、memset、strcpy、strncpy、strcmp、strncmp、strlen
C51/C52 特殊功能寄存器表
三极管原理及特性分析
Tabbar implementation of dynamic bottom navigation bar in uniapp, authority management
Round up a little detail of the round
Interprocess communication - mutex