数组练习

数组练习

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值