当前位置:网站首页>Utilisation de la carte et de foreach dans JS
Utilisation de la carte et de foreach dans JS
2022-07-02 06:40:00 【La santé de la Lune dans la dynastie Qin】
Catalogue des articles
Préface
Il n'y a pas eu de séparation dans le processus de codagemapEtforEachLa différence entre,Reste tranquille,Prenez quelques exemples et étudiez.
Un.、map
1.Tableau simple
const a = [ 5, 7, 10, ]
const mapA = a.map((item, index, arr) => {
console.log(item, index, arr)
return item * 2
})
console.log(mapA)
Produits:
2.Tableau d'objets
const c = [
{
key: 'key1',
value: 'value1',
},
{
key: 'key2',
value: 'value2',
},
]
const mapC = c.map((item, index, arr) => {
console.log(item, index, arr)
let res = this.$deepCopy(item)
res.value += index
return res
})
console.log(c)
console.log(mapC)
Produits:
Donc en faitmapNon seulement vous pouvez traverser le tableau, Vous pouvez également retourner un nouveau tableau .
3.Modifier le contenu du tableau simple
const a = [ 5, 7, 10, ]
const mapA = a.map((item, index, arr) => {
console.log(item, index, arr)
arr[index] = item * 2
return item * 2
})
console.log(a)
console.log(mapA)
Produits:
4.Modifier le contenu du tableau d'objets
const c = [
{
key: 'key1',
value: 'value1',
},
{
key: 'key2',
value: 'value2',
},
]
const mapC = c.map((item, index, arr) => {
console.log(item, index, arr)
let res = item
res.value += index
return res
})
console.log(c)
console.log(mapC)
Produits:
Visible, Tableau d'objets en cours de traversée , Modifier la valeur de l'attribut peut affecter le tableau original
map Pour plus de détails sur l'utilisation de Documentation
2.、forEach
1.Tableau simple
const a = [ 5, 7, 10, ]
a.forEach((item, index, arr) => {
arr[index] = item * 2
console.log(item, index, arr)
})
console.log(a)
Produits:
2.Tableau d'objets
const c = [
{
key: 'key1',
value: 'value1',
},
{
key: 'key2',
value: 'value2',
},
]
c.forEach((item, index, arr) => {
console.log(item, index, arr)
item.value += index
})
console.log(c)
Produits:Visible, Tableau d'objets en cours de traversée , Modifier la valeur de l'attribut peut affecter le tableau original
3.forEach() De continue Avec break
Voir la documentation pour plus de détails
边栏推荐
- Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
- Cglib agent - Code enhancement test
- Présence d'une panne de courant anormale; Problème de gestion de la fsck d'exécution résolu
- Redis - big key problem
- Apt command reports certificate error certificate verification failed: the certificate is not trusted
- Sentinel Alibaba open source traffic protection component
- 阿里云MFA绑定Chrome浏览器
- CUDA中的Warp matrix functions
- Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
- AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组
猜你喜欢
FE - 微信小程序 - 蓝牙 BLE 开发调研与使用
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
Redis——Cluster数据分布算法&哈希槽
Unexpected inconsistency caused by abnormal power failure; Run fsck manually problem resolved
20201002 VS 2019 QT5.14 开发的程序打包
一口气说出 6 种实现延时消息的方案
Apt command reports certificate error certificate verification failed: the certificate is not trusted
Cglib agent - Code enhancement test
Log (common log framework)
随机推荐
AWD学习
Log (common log framework)
Dynamic global memory allocation and operation in CUDA
[self cultivation of programmers] - Reflection on job hunting Part II
Vector types and variables built in CUDA
Nodejs - Express middleware modification header: typeerror [err_invalid_char]: invalid character in header content
日志 - 7 - 记录一次丢失文件(A4纸)的重大失误
Uploading attachments using Win32 in Web Automation
Loops in tensorrt
ZZQ的博客目录--更新于20210601
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
20201025 visual studio2019 qt5.14 use of signal and slot functions
unittest.TextTestRunner不生成txt测试报告
Latex在VSCODE中编译中文,使用中文路径问题解决
构建学习tensorflow
ctf-web之练习赛
PgSQL学习笔记
Function execution space specifier in CUDA
DeprecationWarning: .ix is deprecated. Please use.loc for label based indexing or.iloc for positi
Redis——大Key問題