[英]Swapping a JavaScript array with JSON data in a Search App?
Hello you wonderful people, I have an application in which the script search.js searches a JavaScript array and displays a list of results when a user enters a word into a search bar and the users presses enter. 大家好,大家好,我有一个应用程序,其中的脚本search.js搜索JavaScript数组并在用户在搜索栏中输入单词并按Enter时显示结果列表。 I am trying to switch the array for JSON data in a file artists.json.
我正在尝试在Artists.json文件中为JSON数据切换数组。
function Artist(image, title, link) {
this.image = image;
this.title = title;
this.link = link;
}
var artists = []
artists.push(new Artist("image", "item1", "1.html"));
artists.push(new Artist("image", "item2", "2.html"));
artists.push(new Artist("image", "item3", "3.html"));
artists.push(new Artist("image", "item4", "4.html"));
artists.push(new Artist("image", "item5", "5.html"));
artists.push(new Artist("image", "item6", "6.html"));
var searchBox = document.querySelector("#search");
searchBox.focus();
function doSearch(event) {
var msg = document.querySelector("#msg");
var artistList = document.querySelector("#artist-list");
artistList.innerHTML = "";
var searchTerm = searchBox.value.trim().toLowerCase();
msg.innerHTML = "";
if (event.keyCode === 13) {
if (searchTerm === "") {
msg.innerHTML = "You didn't enter anything";
msg.classList.add("error");
} else {
var count = 0;
for (var i = 0; i < artists.length; i ++) {
if (artists[i].title.toLowerCase().search(searchTerm) !== - 1) {
artistList.innerHTML += "<li><a href='artists/" + artists[i].link + "'>" + artists[i].title + "<a/></li>";
count ++;
}
//else if()
}
msg.innerHTML = "For " + searchTerm + ". We found " + count + " results.";
msg.classList.remove("error");
}
}
}
searchBox.addEventListener("keyup", doSearch, false);
var msgTxt = document.querySelector("#msg");
var artistList = document.querySelector("#artist-list");
var searchBox = document.querySelector("#search");
msgTxt.innerHTML = "";
searchBox.value = "";
searchBox.addEventListener("keyup", doSearch, false);
I want to replace the artists array with JSON data below so I can search through this instead of the Array, I can load the JSON data fine but Im struggling with how to replace the array in the function. 我想用下面的JSON数据替换artists数组,以便我可以搜索它而不是Array,可以很好地加载JSON数据,但是Im在如何替换函数中的数组方面遇到了麻烦。 Any help will be much appreciated.
任何帮助都感激不尽。
[
{
"image":"image1",
"title":"title1",
"link":"link1"
},
{
"image":"image2",
"title":"title2",
"link":"link2"
},
{
"image":"image3",
"title":"title3",
"link":"link3"
},
{
"image":"image4",
"title":"title4",
"link":"link4"
},
{
"image":"image5",
"title":"title5",
"link":"link5"
},
] ]
You want to turn a class into a simple object, the best way is to add a method to the class like this: 您想将一个类变成一个简单的对象,最好的方法是向该类添加一个方法,如下所示:
Artist.prototype.toJson=function() {
return {
image:this.image,
title:this.title,
link:this.link
}
}
then all you need to do to transform the Artist array to a JSON array is artists.map(function(a) { return a.toJson(); })
那么您需要将Artist数组转换为JSON数组的所有步骤是
artists.map(function(a) { return a.toJson(); })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.