第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓數(shù)組中的對象將其讀取為 toUpperCase?

如何讓數(shù)組中的對象將其讀取為 toUpperCase?

吃雞游戲 2022-07-15 10:29:22
此函數(shù)在沒有 .toUpperCase 方法的情況下工作。但我想讓它與 toUpperCase 一起工作。我該怎么辦?我目前正在從 udemy 課程學(xué)習 Javascript:const notes = [    {},{    title: 'My next trip',    body: 'I would like to go to Spain'}, {    title: 'Habbits to work on',    body: 'Excercise, Eat a bit better'}, {    title: 'Office modification',    body: 'Get a new seat'}]function findNote(notes, noteTitle){    const index = notes.findIndex(function(item, index){        return item.title.toUpperCase() === noteTitle.toUpperCase()    })    return notes[index]}const note = findNote(notes, 'Office modification')console.log(note)
查看完整描述

2 回答

?
冉冉說

TA貢獻1877條經(jīng)驗 獲得超1個贊

由于您沒有提供錯誤日志,我只能猜測出了什么問題:您RefernceError在回調(diào)中有一個。如果是這種情況,原因如下:

const notes = [
    {},{

請注意,您在數(shù)組中有一個空對象notes,并且titleitem.title.toUpperCase()評估undefined導(dǎo)致 a 的原因ReferenceError時,只需刪除空對象即可解決此問題。

它沒有工作的原因toUpperCase是因為沒有取消引用title,你只是在它上面使用 === 并不關(guān)心它是否是undefined.


查看完整回答
反對 回復(fù) 2022-07-15
?
精慕HU

TA貢獻1845條經(jīng)驗 獲得超8個贊

您可以添加檢查您的notes數(shù)組對象是否具有屬性title??梢允褂胔asOwnProperty檢查:


if (fooObject.hasOwnProperty('title'))

或者在你的情況下:


item.hasOwnProperty('title')

但是,我們希望找到titlekey 不存在的情況并省略這些對象,因為如果不存在title,則意味著沒有方法toUpperCase()。所以可以用運算符!(NOT)檢查:


if (!item.title) // `NOT` + undefined gives `true`

    return false;

所以整個代碼看起來像這樣:


const notes = [

  {},

  {

      title: 'My next trip',

      body: 'I would like to go to Spain'

  },

  {

      title: 'Habbits to work on',

      body: 'Excercise, Eat a bit better'

  },

  {

      title: 'Office modification',

      body: 'Get a new seat'

  }

]



function findNote(notes, noteTitle) {

  const index = notes.findIndex(function (item, index) {

    if (!item.title)

      return false;


    return item.title.toUpperCase() === noteTitle.toUpperCase()

  })

  return notes[index]

}


const note = findNote(notes, 'Office modification')

console.log(note)



查看完整回答
反對 回復(fù) 2022-07-15
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號