Currently, I'm in the process of developing a basic search system using JavaScript and JSON to fetch data stored within the JSON file. Within this file, multiple 'posts' are listed along with corresponding array of 'tags'. The primary objective is to filter through each post's tags and display only those matching a specific query, for example "funny cat video". To qualify for display, posts must encompass all three tags - "funny", "cat", and "video".
My main concern lies in achieving optimal performance outcomes. Given there are around 2000 posts with each bearing between 5 to 50 tags, executing this task solely through JavaScript may lead to inefficiencies. While seeking guidance on enhancing performance from various sources, additional assistance would significantly aid my progress.
Displayed below is part of the code detailing data storage:
{
"index": {
"count": "2",
"posts": [
{
"id": "1",
"date": "2014-11-21 17:16:39 GMT",
"url": "http://url/",
"image": "http://big_image/",
"thumbnail": "http://little_image/",
"tags": ["funny", "cat", "picture", "falling", "chair", "window sill", "funny"]
},
{
"id": "2",
"date": "2014-11-20 17:57:32 GMT",
"url": "http://url1/",
"image": "http://big_image1/",
"thumbnail": "http://little_image1/",
"tags": ["funny", "cat", "picture", "jumping", "water", "bath", "funny"]
}
]
}
}
Moreover, here is the existing JavaScript implementation:
var query = "funny cat bath".split(" ");
var data = JSON.parse("THE JSON GOES HERE");
var count = data.index.count;
var index = data.index.posts;
for (var i = 0, indexLength = index.length; i < indexLength; i++) {
tags = index[i].tags;
for (var q = 0, queryLength = query.length; q < queryLength; q++) {
if(tags.indexOf(query[q]) !== false) {
console.log(index[i]);
}
}
}
Regrettably, the current code fails to exclusively return posts containing all three specified tags and instead returns duplicates as well. Seeking an improved solution or alternative approach to overcome this roadblock. Any suggestions or guidance would be greatly appreciated!