zoukankan      html  css  js  c++  java
  • 数组练习

    数组练习

        const list = [{
            id: 1,
            name: "刘备"
        },
        {
            id: 2,
            name: "关羽"
        },
        {
            id: 3,
            name: "张飞"
        },
        {
            id: 9,
            name: "孙权"
        },
        {
            id: 4,
            name: "诸葛亮"
        },
        {
            id: 6,
            name: "郭嘉"
        },
        {
            id: 7,
            name: "张辽"
        },
        {
            id: 8,
            name: "荀令君"
        },
        {
            id: 10,
            name: "周瑜"
        },
        {
            id: 14,
            name: "张颌"
        },
        {
            id: 11,
            name: "诸葛恪"
        },
        {
            id: 5,
            name: "曹操"
        },
        {
            id: 12,
            name: "司马懿"
        },
        {
            id: 13,
            name: "司马昭"
        },
        {
            id: 15,
            name: "司马师"
        },
        ]
    
            // 找到id为5的名字
    console.log(list.find((index)=>{return index.id===5}).name)
    
            // 找到名字是郭嘉的索引
    
    console.log(list.findIndex((index)=>{return index.name=="郭嘉"}));
            // 找到所有姓张的人数组
    console.log(list.filter((index)=>{return index.name.startsWith("张")}));
    
            // 找到包含葛字的人的数组
    console.log(list.filter((index)=>{return index.name.includes("葛")}));
            // 找到所有包含司马的人的id数组
            // map的api。 基于现有的数组生成一个新的数组,新的数组中的每一项,就是回调函数中返回的值
    
    console.log(list.filter((index)=>{return index.name.includes("司马")}).map((index1)=>{return index1.id}));
    
    
            // 判断数组中是否存在贾诩
            // some 如果有一个满足条件就返回true,否则返回false
    console.log(list.some((index)=>{return index.name=="贾诩"}));
    
            // 判断数组中是否所有人的id都大于14
    console.log(list.every((index)=>{return index.id>14}));
    

      

  • 相关阅读:
    SSM框架搭建(二) 创建MAVEN项目
    SSM框架搭建(一) JDK和MAVEN环境搭建
    Sublime Text3 手动 配置 NodeJs 环境
    spring-petclinic性能调优实战(转)
    algs4 使用 DrJava 编写 Hello World on Windows
    系统学习数据结构算法
    Algorithm 学习环境准备
    Gradle构建多模块项目
    使用 Gradle 构建 Java 项目
    Gradle目录结构详解
  • 原文地址:https://www.cnblogs.com/wenaq/p/13604493.html
Copyright © 2011-2022 走看看