常用的方法实战

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)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,847评论 0赞 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,428评论 19赞 139
  • 关键词:离婚题主:男问:冷爱,你好。我今年35岁,老婆34岁,我们是经媒人介绍认识的。我大专,她小学。我162cm...
    冷爱阅读 353评论 0赞 0
  • 不知道从什么时候开始,自己也变得斤斤计较起来,从名利到生活中的蝇头小事,平白为自己添了许多烦恼,昨日同一个尊敬的师...
    大脸猫的私喵床铺阅读 303评论 1赞 1
  • 这篇文章主要介绍了使用redis来作为消息队列,并且进行分布式主从配置,需要的朋友可以参考下。查看原文 redis...
    ioiogoo阅读 2,962评论 0赞 6

友情链接更多精彩内容