I want to search through a JSON object which I am fetching from a public api, I have a text input and a button.
My JSON object looks like this
0:{
first_name: "Shkodran"
form: "2.3"
id: 1
news_added: "2020-02-27T23:00:18.104137Z"
points_per_game: "3.2"
second_name: "Mustafi"
web_name: "Mustafi"
minutes: 620
goals_scored: 0
assists: 2
clean_sheets: 2
goals_conceded: 9
own_goals: 0
penalties_saved: 0
penalties_missed: 0
yellow_cards: 0
red_cards: 0
saves: 0
}
Each entry is a different player and there are 628 entries.
When the button is clicked I would like to be able to search within the data in the JSON object and check whether the data value the user has input exists in the JSON object.
If the input text matches the data value within the JSON the form of the player will logged to the console
HTML
<form id="players">
<input type="text" id ="search_players" placeholder="Search for a player...">
<button type="submit" id="submit" onclick="playersearch()"></button>
</form>
Javascript
const input = document.getElementById('search_players');
const searchfield = "first_name" + "second_name";
playersearch = function() {
var playername = input.value;
for (var i=0 ; i < players.length ; i++)
{
if (players[i].first_name.second_name.indexOf(playername)) {
console.log(players[i].form);
}
}
}