javascript - 在 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)
解决方案
你的代码没问题。请在此处检查您的函数参数:
const note = findNote(notes, 'Fitness Goals')
没有任何标题为“健身目标”的注释。将此字符串更改为:
const note = findNote(notes, 'Fitness goals')
'Goals'和'goals'在 Javascript 中是不同的字符串,所以程序找不到任何注释
推荐阅读
- ios - 检查用户连续使用应用程序的天数
- r - R - 从字符列中的值创建动态指标列
- time-complexity - 从 0 到 n 的所有斐波那契数的时间复杂度
- python - 为什么 scipy.spatial.ckdtree 运行速度比 scipy.spatial.kdtree 慢
- r - 是什么使负数成为lubridate?
- sql - 在 Active Record 中查询数据库之前删除空格在 Rails C 中有效,但在控制器中无效
- javascript - 在加载数据时如何在 react-native FlatList 中呈现 isLoading 元素
- asp.net - 为什么我在 Azure blob 存储中的文本文件中的文本在下载时格式不正确?
- vmware - 我们应该如何决定 VMWare 所需的虚拟处理器和 RAM 的数量
- python - 在 Google Colab 平台上的 Jupyter Notebook 中显示/渲染 HTML 文件