首页 > 编程语言 >[JavaScript-02]FOR WHILE循环

[JavaScript-02]FOR WHILE循环

时间:2022-11-09 23:11:30浏览次数:43  
标签:02 map function text JavaScript filter WHILE isCompleted todos

1. 语句

// For语句
for (let index = 0; index <=6; index++) 
{
    console.log(`For Loop Number:${index}`);    
}

// While语句
let i = 0;
while (i <=6) {
    console.log(`While Loop Number:${i}`);
    i++;
}

2. for应用

// For 应用
const todos =
[
    {
        id          : 1,
        text        : 'Take out trash',
        isCompleted : true
    },
    {
        id          : 2,
        text        : 'Meeting with boss',
        isCompleted : true
    },
    {
        id          : 3,
        text        : 'Dentist appt',
        isCompleted : false
    },
];
// 索引遍历
for (let i=0;i<todos.length;i++) 
{
    console.log('todos[i].text:',todos[i].text);
}
// 内容遍历
for (let t of todos)
{
    console.log('t.id:',t.id);
}

3. forEach, map, filter

// forEach
todos.forEach
(
    function(t)
    {
        console.log('t.text:',t.text);
    }
);
// map 和 filter 的区别 map返回数组,filter返回符合条件的数组
// map 返回数组
const t = todos.map
(
    function (t) 
    {
        return t.id;    
    }
);
console.log('t:',t);

// filter过滤器
// === Python ==
const tCompleted = todos.filter
(
    function(t)
    {
        return t.isCompleted === true;
    }
);
console.log('tCompleted:',tCompleted);

const ttCompleted = todos.filter
(
    function(t)
    {
        return t.isCompleted === true;
    }
).map
(
    function(t)
    {
        return t.text;
    }
);
console.log('ttCompleted:',ttCompleted);

END

标签:02,map,function,text,JavaScript,filter,WHILE,isCompleted,todos
From: https://www.cnblogs.com/leoshi/p/16875544.html

相关文章

  • 2022-11-9学习内容
    1.案例-购物车-数据库准备1.1MyApplication.java改为内部存储私有空间//内部存储私有空间Stringdirectory=getFilesDir().toString()+File.separatorCh......
  • ACdream 1028 Path
    DescriptionCheckifthereexistsapathoflength ll inthegiventreewithweightassignedtoeachedges.InputThefirstlinecontainstwoin......
  • 2022.11.9
    ###noip模拟为什么一点儿进步都没有啊。。。怎么还越来越菜了。。。。。。  ##出错点t1:MLE。。。。。。也是挺牛t4://intans=0;longlongans=0;//n*n啊不......
  • CSP-S2022 总结
    调整了下心态开考顺序开题看完\(T1,T2\)直接开打\(T2\)的线段树,还是比较好写的然后思考先打\(T1\)呢还是拍\(T2\),最后决定拍\(T2\),稳一点发现随机数据很弱,决定......
  • 【2022-11-09】luffy项目实战(四)
    一、前台首页组件编写#HomeView.vue页面组件#Header.vue头部组件#Banner.vue轮播图组件#Footer.vue尾部组件1.1HomeView.vue<template><divcla......
  • JavaScript
    1、什么是JavaScript?JavaScript是一门世界上最流行的脚本语言严格检查:'usestrict'<script>'usestrict'//必须放在第一方行leti=1;</script>2、数据......
  • #yyds干货盘点#【愚公系列】2022年11月 微信小程序-导航(跳转)
    前言1.navigatornavigator是页面跳转的标签,具体参数如下:属性类型默认值必填说明最低版本targetstringself否在哪个目标上发生跳转,默认当前小程序2.0.7......
  • 2022.11.09 NOIP2022 模拟赛六
    科学Source:CF461CApplemanandaSheetofPaper,*2200。注意到对于\(p\le\lfloor\frac{now}{2}\rfloor\),直接暴力维护的复杂度是对的。而对于其\(>\)的情况,翻转......
  • IDEA2022.2.3创建maven项目
    1、打开IDEA,选择file-new-project  2、项目配置  3、整体的一个目录结构  4、idea中maven的配置,打开file-settings ......
  • JavaScript函数
     JavaScript函数JavaScript函数概念    可以储存一段代码的数据类型- 分为两个阶段:函数定义阶段和函数调用阶段- 1.函数定义阶段:把代码放进“盒子”,函数里面代......