当前位置:网站首页>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
边栏推荐
- FE - Eggjs 结合 Typeorm 出现连接不了数据库
- Cglib agent - Code enhancement test
- Eggjs -typeorm 之 TreeEntity 实战
- Sparse array (nonlinear structure)
- Dynamic global memory allocation and operation in CUDA
- 2020-9-23 use of QT timer qtimer class.
- 记录一次RDS故障排除--RDS容量徒增
- FE - Weex 使用简单封装数据加载插件为全局加载方法
- Thread hierarchy in CUDA
- 提高用户体验 防御性编程
猜你喜欢
ctf三计
Learn about various joins in SQL and their differences
Latex在VSCODE中编译中文,使用中文路径问题解决
[literature reading and thought notes 13] unprocessing images for learned raw denoising
PgSQL学习笔记
Cglib agent - Code enhancement test
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
蚂蚁集团g6初探
No process runs when querying GPU, but the video memory is occupied
Sentinel Alibaba open source traffic protection component
随机推荐
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
eslint配置代码自动格式化
ShardingSphere-JDBC篇
Automation - when Jenkins pipline executes the nodejs command, it prompts node: command not found
FE - Weex 使用简单封装数据加载插件为全局加载方法
Three suggestions for all students who have graduated and will graduate
Self cultivation of programmers - Reflection on job hunting
提高用户体验 防御性编程
[self cultivation of programmers] - Reflection on job hunting Part II
构建学习tensorflow
TensorRT的数据格式定义详解
重载全局和成员new/delete
CUDA中的线程层次
Linked list (linear structure)
Fe - weex uses a simple encapsulated data loading plug-in as the global loading method
20201002 vs 2019 qt5.14 developed program packaging
Sentinel Alibaba open source traffic protection component
Name six schemes to realize delayed messages at one go
由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
底层机制Mvcc