常用的方法實戰(zhàn)

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.把數(shù)組的對象元素的某一項拼接成字符串

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

6.完成下列數(shù)組的常用操作

問題

 let todos = [
  {
    id: 1001,
    title: '請找出這個數(shù)組中第一個id為1005的todo的位置',
    completed: false
  },
  {
    id: 1002,
    title: '請找出這個數(shù)組中所有completed為true的todo',
    completed: true
  },
  {
    id: 1005,
    title: '這個數(shù)組中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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,679評論 0 4
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評論 19 139
  • 關(guān)鍵詞:離婚題主:男問:冷愛,你好。我今年35歲,老婆34歲,我們是經(jīng)媒人介紹認(rèn)識的。我大專,她小學(xué)。我162cm...
    冷愛閱讀 313評論 0 0
  • 不知道從什么時候開始,自己也變得斤斤計較起來,從名利到生活中的蠅頭小事,平白為自己添了許多煩惱,昨日同一個尊敬的師...
    大臉貓的私喵床鋪閱讀 276評論 1 1
  • 這篇文章主要介紹了使用redis來作為消息隊列,并且進(jìn)行分布式主從配置,需要的朋友可以參考下。查看原文 redis...
    ioiogoo閱讀 2,937評論 0 6

友情鏈接更多精彩內(nèi)容