数组练习

    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}));

  

 

 posted on 2020-09-02 21:26  wen22  阅读(93)  评论(0编辑  收藏  举报