Algolia 显示 Javascript 的数组值
Algolia show array values with Javascript
所以我正在使用 Algolia 的即时搜索并且非常喜欢它,但是我遇到了一个问题。我的数据库模型有一个多态关联,其中每个 tag
都存储在一个数组中,我想简单地将所有标签显示为一个字符串,但我不知道该怎么做。我试过获取 tag_list.value,但那没有用,其他一切都变得未定义。
<script>
var client = algoliasearch("xxxxx", "xxxx");
var index = client.initIndex('myname');
//initialize autocomplete on search input (ID selector must match)
autocomplete('#aa-search-input',
{ hint: false }, {
source: autocomplete.sources.hits(index, {hitsPerPage: 25}),
//value to be displayed in input control after user's suggestion selection
displayKey: 'name',
//hash of templates used when rendering dataset
templates: {
//'suggestion' templating function used to render a single suggestion
suggestion: function(suggestion) {
return '<span class="aa-highlight">' +
suggestion._highlightResult.tag_list.value +
suggestion._highlightResult.question.value + '</span><span>' +
suggestion._highlightResult.answer_explanation.value + '</span>';
}
}
});
</script>
tag_list 是一个数组,但我不知道如何访问 javascript 中的 json 数组是我要问的。我尝试了 .stringify 和 .arr[0],但它们是未定义的。
suggestion._highlightResult.tag_list.value +
这是我遇到问题的相关代码/代码行。
跟进 arieljuod 的评论,了解 suggestion._highlightResult.tag_list.value
返回的是什么类型会有所帮助。话虽如此,我希望以下内容对您有所帮助。
数组到字符串
在 JavaScript 中,您可以通过调用数组的 join
方法将数组转换为字符串。
例子
单项数组
var array = ["item 1"];
array.join(); // Output: "item 1"
多项数组
var array = ["item 1", "item 2"];
array.join(); // Output: "item 1,item 2"
自定义分隔符
默认情况下,join
方法使用“,”作为数组中每一项的分隔符。如果你想使用不同的东西,那么你可以将一个字符串传递给 join
函数:
var array = ["item 1", "item 2"];
array.join("/"); // Output: "item 1/item 2"
JSON数组到JavaScript数组
我包括这个是因为我不确定从 suggestion._highlightResult.tag_list.value
.
返回的数据类型是什么
如果从 tag_list.value
返回的值是一个包含 JSON 的字符串(即 "[\"item 1\", \"item 2\"]"
那么您可以使用 JSON.parse()
将其转换为 JavaScript数组:
var json_string = "[\"item 1\", \"item 2\"]";
JSON.parse(json_string); // Output: ["item 1", "item 2"]
所以我正在使用 Algolia 的即时搜索并且非常喜欢它,但是我遇到了一个问题。我的数据库模型有一个多态关联,其中每个 tag
都存储在一个数组中,我想简单地将所有标签显示为一个字符串,但我不知道该怎么做。我试过获取 tag_list.value,但那没有用,其他一切都变得未定义。
<script>
var client = algoliasearch("xxxxx", "xxxx");
var index = client.initIndex('myname');
//initialize autocomplete on search input (ID selector must match)
autocomplete('#aa-search-input',
{ hint: false }, {
source: autocomplete.sources.hits(index, {hitsPerPage: 25}),
//value to be displayed in input control after user's suggestion selection
displayKey: 'name',
//hash of templates used when rendering dataset
templates: {
//'suggestion' templating function used to render a single suggestion
suggestion: function(suggestion) {
return '<span class="aa-highlight">' +
suggestion._highlightResult.tag_list.value +
suggestion._highlightResult.question.value + '</span><span>' +
suggestion._highlightResult.answer_explanation.value + '</span>';
}
}
});
</script>
tag_list 是一个数组,但我不知道如何访问 javascript 中的 json 数组是我要问的。我尝试了 .stringify 和 .arr[0],但它们是未定义的。
suggestion._highlightResult.tag_list.value +
这是我遇到问题的相关代码/代码行。
跟进 arieljuod 的评论,了解 suggestion._highlightResult.tag_list.value
返回的是什么类型会有所帮助。话虽如此,我希望以下内容对您有所帮助。
数组到字符串
在 JavaScript 中,您可以通过调用数组的 join
方法将数组转换为字符串。
例子
单项数组
var array = ["item 1"];
array.join(); // Output: "item 1"
多项数组
var array = ["item 1", "item 2"];
array.join(); // Output: "item 1,item 2"
自定义分隔符
默认情况下,join
方法使用“,”作为数组中每一项的分隔符。如果你想使用不同的东西,那么你可以将一个字符串传递给 join
函数:
var array = ["item 1", "item 2"];
array.join("/"); // Output: "item 1/item 2"
JSON数组到JavaScript数组
我包括这个是因为我不确定从 suggestion._highlightResult.tag_list.value
.
如果从 tag_list.value
返回的值是一个包含 JSON 的字符串(即 "[\"item 1\", \"item 2\"]"
那么您可以使用 JSON.parse()
将其转换为 JavaScript数组:
var json_string = "[\"item 1\", \"item 2\"]";
JSON.parse(json_string); // Output: ["item 1", "item 2"]