admin管理员组文章数量:1393049
I want to make a live search with JSON data from star wars api. The JSON contain of some objects
, and the objects
I wanted to fetch is results
, which is an array
that contains objects
that have propertie name
,height
,mass
, ect.
The snippet of the JSON like this:
{
"count": 87,
"next": "/?format=json&page=2",
"previous": null,
"results": [
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "/",
"films": [
"/",
"/",
"/",
"/",
"/"
],
"species": [
"/"
],
"vehicles": [
"/",
"/"
],
"starships": [
"/",
"/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "/"
}
]
}
I tried with this code but the console says Uncaught TypeError: Cannot read property 'name' of undefined
(on line 7)
$('#search').keyup(function() {
var searchField = $('#search').val();
var myExp = new RegExp(searchField, "i");
$.getJSON('/?format=json', function(data) {
var output = '<ul class="searchresults">';
$.each(data, function(key, val) {
if ((val.results.name.search(myExp) != -1) ||
(val.results.height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ val.name +'</h2>';
output += '<p>'+ val.height +'</p>';
output += '</li>';
}
});
output += '</ul>';
$('#update').html(output);
});
});
I know there's something wrong with my loop, I tried looking for the the answer but I can't get the right answer. Can you please help me?
I want to make a live search with JSON data from star wars api. The JSON contain of some objects
, and the objects
I wanted to fetch is results
, which is an array
that contains objects
that have propertie name
,height
,mass
, ect.
The snippet of the JSON like this:
{
"count": 87,
"next": "http://swapi.co/api/people/?format=json&page=2",
"previous": null,
"results": [
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "http://swapi.co/api/planets/1/",
"films": [
"http://swapi.co/api/films/6/",
"http://swapi.co/api/films/3/",
"http://swapi.co/api/films/2/",
"http://swapi.co/api/films/1/",
"http://swapi.co/api/films/7/"
],
"species": [
"http://swapi.co/api/species/1/"
],
"vehicles": [
"http://swapi.co/api/vehicles/14/",
"http://swapi.co/api/vehicles/30/"
],
"starships": [
"http://swapi.co/api/starships/12/",
"http://swapi.co/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "http://swapi.co/api/people/1/"
}
]
}
I tried with this code but the console says Uncaught TypeError: Cannot read property 'name' of undefined
(on line 7)
$('#search').keyup(function() {
var searchField = $('#search').val();
var myExp = new RegExp(searchField, "i");
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
var output = '<ul class="searchresults">';
$.each(data, function(key, val) {
if ((val.results.name.search(myExp) != -1) ||
(val.results.height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ val.name +'</h2>';
output += '<p>'+ val.height +'</p>';
output += '</li>';
}
});
output += '</ul>';
$('#update').html(output);
});
});
I know there's something wrong with my loop, I tried looking for the the answer but I can't get the right answer. Can you please help me?
Share Improve this question asked Jan 16, 2017 at 8:32 bnrflybnrfly 1652 gold badges2 silver badges12 bronze badges 4-
Val
inundefined
, thusval.name
does not work. Have you verified that the endpoint give you well formatted json? – Automatico Commented Jan 16, 2017 at 8:36 - results is array and not object property you must iterate results to acess name – Akshay Commented Jan 16, 2017 at 8:36
-
@Akshaypadwal yeah, I said
results
is array that contain objects – bnrfly Commented Jan 16, 2017 at 8:40 - then you must iterate as i show below – Akshay Commented Jan 16, 2017 at 8:42
5 Answers
Reset to default 3It's a simple mistake, you should iterate over data.results
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
$.each(data.results, function(key, val) {
console.log(val.name);
})
});
Your JSON contains :
- "count": 87,
- "next": "http://swapi.co/api/people/?format=json&page=2",
- "previous": null,
- "results": The data you want
So you must get results and iterate over it.
You need to iterate over the results
of the data
object like this:
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
$.each(data.results, function(key, value){
console.log("key", key);
console.log("value", value.name)
})
})
Or in your case:
$('#search').keyup(function() {
var searchField = $('#search').val();
var myExp = new RegExp(searchField, "i");
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
var output = '<ul class="searchresults">';
$.each(data.results, function(key, val) { //Only this line changes.
if ((val.results.name.search(myExp) != -1) ||
(val.results.height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ val.name +'</h2>';
output += '<p>'+ val.height +'</p>';
output += '</li>';
}
});
output += '</ul>';
$('#update').html(output);
});
});
I think you should have something like:
output += '<h2>'+ val.results.name +'</h2>';
output += '<p>'+ val.results.height +'</p>';
exactly as you have in your if
condition
Make your $.each
loop as below:Because val.results
is in array format of json.
$.each(data, function(key, val) {
var results = val.results;
$.each(results,function(index,value){
if ((value[index].name.search(myExp) != -1) ||
(value[index].height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ value[index].name +'</h2>';
output += '<p>'+ value[index].height +'</p>';
output += '</li>';
}
});
});
Hope it will works correctly.
OR simply
$.each(data.results,function(index,value){
if ((value[index].name.search(myExp) != -1) ||
(value[index].height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ value[index].name +'</h2>';
output += '<p>'+ value[index].height +'</p>';
output += '</li>';
}
});
May be enough for you.
try this
var a = //your json;
$.each(a,function(i,val){
if(val.results.length != 0){
$.each(val.results,function(k,value){
console.log(value.name)})
}
})
本文标签: javascriptHow to loop through JSON object with each JQueryStack Overflow
版权声明:本文标题:javascript - How to loop through JSON object with $.each JQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744585878a2614192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论