0

So, Im trying to deal with an API. i successful load the json(which is an array of objects) in my browser like the following

0:
source: {id: null, name: "Protothema.uk"}
author: "james bond"
title: " A TITLE"
description: "A DESCRIPTION"
__proto__: Object

my code

$.getJSON("http://newsapi.org/v2/top-headlines?country=uk&category=health&apiKey=MYAPIKEY", function(data){
   //console.log(data);

   $.each(data,function(index,value){
       console.log(value);

    console.log(value[0]);       
     console.log(value[0].title)//Cannot read property 'title' of undefined
   });

});



when i try to print the whole index like console.log(value[0]); i successfully get all the objects of the index 0.

but when i try to print a specific value of key like console.log(value[0].title) i get Cannot read property 'title' of undefined

im stuck for hours, what im doing wrong?

  • The array only has 1 element, so there's no `value[1]`. – Barmar Apr 17 '20 at 22:45
  • Try `value[0].title` – Barmar Apr 17 '20 at 22:45
  • `value[0]`... not `value[1]` – Mosia Thabo Apr 17 '20 at 22:46
  • @Barmar im sorry, i edit it. not ```value[1]``` i mean i still want to get ```console.log(value[0].title)``` – TryMySkills Apr 17 '20 at 22:50
  • Now I'm guessing that you're trying to print it before the API call has returned. Remember that API calls are asynchronous, you should print the result in the callback function, not right after making the call. – Barmar Apr 17 '20 at 22:52
  • @MosiaThabo i just edited bro, my fault – TryMySkills Apr 17 '20 at 22:52
  • But unless you show your actual code, it's just a guess. – Barmar Apr 17 '20 at 22:52
  • See https://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron – Barmar Apr 17 '20 at 22:52
  • Thank you ,Im new to the 'api world'. So where i can find this callback function or anything that will be usefull – TryMySkills Apr 17 '20 at 22:53
  • That's impossible then. If value[0] prints an object with title key and yet when you access the key all of a sudden value[0] is undefined? – Mosia Thabo Apr 17 '20 at 22:53
  • @MosiaThabo The console has live objects, so the array can change before you expand it. – Barmar Apr 17 '20 at 22:57
  • @Barmar Correct, that's possible. But I am confused by his wording. The thing is `data : Object[]` so that means every item on `data` is actually an `object`. So in his case, he's treating item as another array, which is kind of confusing taking into account his opening statement on the question. – Mosia Thabo Apr 17 '20 at 23:17

1 Answers1

0

Structure of Response data: enter image description here

Based on the structure, try access title of every article:

// Using JQuery.each() 
$.each(data.articles,function(index, article){
  console.log(article.title); // to access title of each article.
});
Mosia Thabo
  • 4,009
  • 1
  • 14
  • 24
  • im already using ```console.log(value)``` and prints all the object. However ```value.title``` gives : undefined – TryMySkills Apr 17 '20 at 23:11
  • Please check my answer and try the using `Object.keys` to check the authenticity of that object before using it? – Mosia Thabo Apr 17 '20 at 23:16
  • the ```0:``` is an index of the array that contains an object. this object has some elements, ok? one of them is ```title: " A TITLE"``` so value of the name title is a string – TryMySkills Apr 17 '20 at 23:17
  • Can you please console data and show me it's structure? I agree with what you're saying but it's what you explained on the question. I think seeing the structure of data would help – Mosia Thabo Apr 17 '20 at 23:18
  • Based on what you just said, then I'm not convinced that `data: Object[]`, data is not a list of object because data[0] == value, and if you're saying value also has an index:0, it means value is also an array – Mosia Thabo Apr 17 '20 at 23:19
  • You can insert a picture of `console.log(data)` please on your question. – Mosia Thabo Apr 17 '20 at 23:20
  • this is the result of the console.log(data) https://ibb.co/c14wGVn – TryMySkills Apr 17 '20 at 23:26
  • Cool thanks, so data is equal that whole object which has property `articles of type Object[]`... so could you try `console.log(data.articles[0])` and `console.log(data.articles[0].title)` – Mosia Thabo Apr 17 '20 at 23:33
  • 1
    my man you are awesome, thank you so much for your time and your patience. This world needs more people like you. Again thanks <3 – TryMySkills Apr 17 '20 at 23:38
  • Pleasure my man. I've wasted days before in codes like this and I was helped, someone did the same thing. Patient and never lost hope even though I was getting irritated by their multiple questions. So since then I knew that you must always be patient with people. – Mosia Thabo Apr 17 '20 at 23:39