首页 > 解决方案 > 在 Javascript 数组中找不到对象属性

问题描述

我目前正在学习 JavaScript,并且我在数组中的对象上度过了一段可怕的时光。

我正试图找到他们。它不会找到。

我正在尝试显示它们,它们显示不正确。

我一直在尝试解决这个问题大约 2 天,但我迷路了

这是代码

const notes = [{
    title: 'The big trip',
    body: ' The next big trip will be back to thailand'
  },
  {
    title: 'Fitness goals',
    body: 'really enjoying the ab programme '
  },
  {
    title: 'life decisions',
    body: 'the move overseas '
  }
]

console.log(notes.length)

const findNote = function(notes, noteTitle) {
  const index = notes.findIndex(function(note, index) {
    return note.title === noteTitle
  })

  return notes[index]
}

const note = findNote(notes, 'Fitness Goals')
console.log(notes)

标签: javascriptarrays

解决方案


你的代码没问题。请在此处检查您的函数参数:

const note = findNote(notes, 'Fitness Goals')

没有任何标题为“健身目标”的注释。将此字符串更改为:

const note = findNote(notes, 'Fitness goals')

'Goals''goals'在 Javascript 中是不同的字符串,所以程序找不到任何注释


推荐阅读