如何搜索嵌套字典而不关心顶级键
How to search a nested dictionary and not care about top level key
所以我在我的 Jekyll 应用程序中使用 lunr.js 来实现搜索功能和正确的结果 return。目的是在浏览器中显示结果列表。
我在这里得到结果,然后将它们传递给处理 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 的新手,所以如果措辞不当,请告诉我。
您可以使用 Object.values()
来仅访问对象的值。示例:
const myObj = {a: 1, b: 2}
Object.values(myObj) // => [1, 2]
你抓取第一个键window.store然后你就可以得到你想要的值
const { title, category, url } = window.store[Object.keys(window.store)[0]];
所以我在我的 Jekyll 应用程序中使用 lunr.js 来实现搜索功能和正确的结果 return。目的是在浏览器中显示结果列表。
我在这里得到结果,然后将它们传递给处理 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 的新手,所以如果措辞不当,请告诉我。
您可以使用 Object.values()
来仅访问对象的值。示例:
const myObj = {a: 1, b: 2}
Object.values(myObj) // => [1, 2]
你抓取第一个键window.store然后你就可以得到你想要的值
const { title, category, url } = window.store[Object.keys(window.store)[0]];