美文网首页
常用的方法实战

常用的方法实战

作者: sunny519111 | 来源:发表于2017-07-06 18:19 被阅读23次
1. 找出是否有人超过 19 岁?
 const people = [
      { name: 'Wes', year: 1988 },
      { name: 'Kait', year: 1986 },
      { name: 'Irv', year: 1970 },
      { name: 'Lux', year: 2015 }
 ]
function some(arr){
   return arr.some(item => (new Date().getFullYear() - item.year) >= 19)
}
2.是否所有人都是成年人
 const people = [
      { name: 'Wes', year: 1988 },
      { name: 'Kait', year: 1986 },
      { name: 'Irv', year: 1970 },
      { name: 'Lux', year: 2015 }
 ]
function every(arr) {
    return arr.every(item =>  (new Date().getFullYear() - item.year) >= 18)
}
3.找到 ID 号为 823423 的评论
const comments = [
      { text: 'Love this!', id: 523423 },
      { text: 'Super good', id: 823423 },
      { text: 'You are the best', id: 2039842 },
      { text: 'Ramen is my fav food ever', id: 123523 },
      { text: 'Nice Nice Nice!', id: 542328 }
 ];
function find(arr){
  return arr.find(item => item.id ===823423 )
}

4.删除 ID 号为 823423 的评论

const comments = [
      { text: 'Love this!', id: 523423 },
      { text: 'Super good', id: 823423 },
      { text: 'You are the best', id: 2039842 },
      { text: 'Ramen is my fav food ever', id: 123523 },
      { text: 'Nice Nice Nice!', id: 542328 }
 ];
function findIndex(arr){
   return arr.findIndex(item => item.id ===823423)
}
const index = findIndex(comments)
comments.splice(index,1)

5.把数组的对象元素的某一项拼接成字符串

const goodImgs = [
  {url: 'www.hcc.com'},
  {url: 'www.yx.com'}
]
var strImages = goodImgs.reduce((str,item)=>{
  return  str+`![](${item.url})`
},'')

6.完成下列数组的常用操作

问题

 let todos = [
  {
    id: 1001,
    title: '请找出这个数组中第一个id为1005的todo的位置',
    completed: false
  },
  {
    id: 1002,
    title: '请找出这个数组中所有completed为true的todo',
    completed: true
  },
  {
    id: 1005,
    title: '这个数组中todo的completed都是true吗',
    completed: false
  },
  {
    id: 1004,
    title: '给所有todo的id加10',
    completed: true
  },
  {
    id: 1003,
    title: '计算所有todo的id的和',
    completed: false
  }
] 

答案

todos.findIndex((todo) => {
  return todo.id === 1005
})

todos.filter((todo) => {
  return todo.completed
})

todos.every((todo) => {
  return todo.completed
})

todos.map((todo) => {
  todo.id+=10
  return todo
})

todos.reduce((total,todo) => {
  return total+todo.id;
},0)

相关文章

  • 2018-10-08NodeJs的笔记(2)

    主要记录一下此次对于Buffer的常用静态方法和常用实例方法,为后续的实战铺垫 setImmediate与proc...

  • 常用的方法实战

    1. 找出是否有人超过 19 岁? 2.是否所有人都是成年人 3.找到 ID 号为 823423 的评论 4.删除...

  • rocketmq总目录

    实战 rocketmq最简单的入门demo rocketmq的常用概念,接口和方法 rocketmq的正式部署 高...

  • Android开发学习——Day22(布局方式&实战:手势解锁)

    学习目的 1.了解常用的几种布局方式 2.实战:手势解锁 学习过程 逐一使用不同的布局方式及其属性和方法,在实战项...

  • 初识Docker逃逸

    前言 前不久看到几篇实战文章用到了docker逃逸技术,自己之前没接触过,整理复现下常用的Docker逃逸方法,可...

  • 关于block(2)

    标签: iOS 技术 接上一篇,我们继续探究block。 block作为参数传递 这在实战中定义方法传递值时经常用...

  • 集合

    1. 常用容器类 Collection 的常用方法 List 的常用方法 Set 的常用方法 Map 的常用方法 ...

  • 实战|教你用Python玩转Redis

    今天就来给大家讲解一下Python如何使用Redis,并进行相关的实战操作。 提示:本文讲解了Redis常用的方法...

  • 教你几招玩单反-- 画面布局和测光(4)

    单反实战之画面布局 介绍下我自己常用的几个简单方法: 方法一:经典的黄金分割法 图中ABCD为四个黄金分割的关键点...

  • 常用异动分析方法

    目标: 1、了解常用异动分析方法 2、学会在分析实战中应用 如何分析出异常波动的原因 工作场景:已知某指标异常(以...

网友评论

      本文标题:常用的方法实战

      本文链接:https://www.haomeiwen.com/subject/hdvhhxtx.html