Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

常用的方法实战 #28

Open
huangchucai opened this issue Aug 11, 2017 · 0 comments
Open

常用的方法实战 #28

huangchucai opened this issue Aug 11, 2017 · 0 comments

Comments

@huangchucai
Copy link
Owner

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant