1
I have a headache that is: I need to make a filter by names of the following JSON (summarized)...
[
{
"index": 0,
"age": 25,
"eyeColor": "green",
"name": "Peck Murphy",
"gender": "male",
"company": "MEDICROIX",
"email": "[email protected]",
"phone": "+1 (992) 428-2202",
"address": "219 McDonald Avenue, Tioga, Utah, 6059"
},
{
"index": 1,
"age": 29,
"eyeColor": "blue",
"name": "Rosalyn Mckay",
"gender": "female",
"company": "COREPAN",
"email": "[email protected]",
"phone": "+1 (927) 507-3490",
"address": "537 Rost Place, Thynedale, Wyoming, 5160"
}
]
The ideal would be to filter it by name. It looks simple, but my task is to make this filter from the same JSON, only from an external link, which is this one and then put the lines that passed through the filter into a table.
My question is how do I filter this online JSON by a value inserted in a text box and show all the data of the line that passed through the filter? I have already turned over the entire jQuery manual and nothing that will save me. If someone can give me a light, I will be very grateful.
The latest jQuery script I’ve made:
$(document).ready(function () { // Filtro único para nome
$('#Cons_Name').keyup( function () { // Cons_Name é a id de um input que o usuário insere o nome de alguém para filtro
var json = $.getJSON('https://quarkbackend.com/getfile/gcpolidoro/data-json', function(data) {
var arr = $("td").filter(function (ret, i) {
return ret.name == $("#Cons_Name").val();
})
console.log(arr);
});
});
});
On this last attempt, did you receive an error message? What was the output of the console.log(arr) command? Note that you are not iterating over JSON values but rather a table that is in your HTML.
– jlHertel
The output was "Undefined". What do you mean by "iterating" (sorry, I’m kind of new in this area)
– Gabriel Polidoro
Your loop with the function
filter()
is using other variables that have no relation to JSON– jlHertel
Hm. Maybe I’m wrong about that. I’ll take a look
– Gabriel Polidoro
By correcting the filter from "td" to json (the variable), this appeared here on the console:
[prevObject: n.fn.init[1], context: undefined]
. I think I’m on the right track?– Gabriel Polidoro
Modify your question to reflect the code you are trying.
– jlHertel
Which filter do you apply to the JSON output? What attributes do you want to display?
– Maurivan
I updated the description of the question...
– Gabriel Polidoro
I managed to filter. I’ll leave the JS here as an answer.
– Gabriel Polidoro