首页 > 解决方案 > 如何搜索嵌套字典而不关心顶级键

问题描述

所以我在我的 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 很陌生,所以如果措辞不好,请告诉我。

标签: javascriptdictionaryjekyll

解决方案


你抓住 window.store 的第一个键,然后你可以得到你想要的值

const { title, category, url } = window.store[Object.keys(window.store)[0]];

推荐阅读