javascript - 如何搜索嵌套字典而不关心顶级键
问题描述
所以我在我的 Jekyll 应用程序中使用 lunr.js 来实现搜索功能并且结果正确返回。目的是然后在浏览器中显示结果列表。
在这里我得到结果,然后将它们传递给处理 DOM 操作的函数。
var results = idx.search(searchTerm); // Get lunr to perform a search
displaySearchResults(results, window.store);
的日志console.log(window.store);
如下:
"python-2019-03-23-welcome-to-jekyll-html": Object { title: "test", category: "Python", url: "/python/2019/03/23/welcome-to-jekyll.html" }
第一个键python-2019-03-23-welcome-to-jekyll-html
无关紧要,因为它会根据帖子的数量而变化。我怎样才能得到 的值Object{}
?
任何意见,将不胜感激。我对 JS 很陌生,所以如果措辞不好,请告诉我。
解决方案
你抓住 window.store 的第一个键,然后你可以得到你想要的值
const { title, category, url } = window.store[Object.keys(window.store)[0]];
推荐阅读
- flutter - flutter_bloc :使 initialState 方法异步
- android - 如何在 Android OS 9 上连接和打开 USB 摄像头?
- c++ - 我想制作彼此不同但重复的数字的数字
- python-3.x - 如何使用单个按钮开始和停止录制
- flutter - 在 initState 中访问 InheritedWidget 数据
- html2canvas - html2canvas 每次生成不同的结果
- php - .htaccess 代码阻止所有直接访问,但允许通过 include / cron_jobs / ajax 请求进行内部访问
- arrays - excel VBA中的数据验证列表
- python - Pandas:创建带有产品列表组的表格
- python - 从 csv 中透视数据并存储在数据框中