I am trying to get a single value from a json object. E.g to get only 'authors' not the whole object
Here is my current code:
const getArticlesFromApi = async () => {
let response = await fetch(
'http://api.mediastack.com/v1/news'
)
let test = await response.json();
console.log(test)
}
What can be done to the above code to achieve the result I want and here is the current output:
Object {
"author": "OdAdmin",
"category": "general",
"country": "us",
"description": "New Delhi: Overcoming all hurdles and finding new solutions, Indian Railways is continuing its journey of bringing relief by delivering Liquid Medical Oxygen(LMO) to various states across the country. Oxygen Expresses crossed mark of 26000 MT of LMO delivery in service to the Nation. So far, Indian Railways has delivered more than 26891 MT of […]",
"image": null,
"language": "en",
"published_at": "2021-06-07T15:37:44+00:00",
"source": "orissadiary",
"title": "Oxygen Expresses deliver more than 26891 MT of LMO to the Nation",
"url": "https://orissadiary.com/oxygen-expresses-deliver-more-than-26891-mt-of-lmo-to-the-nation/",
},
Object {
"author": "Graham Gremore",
"category": "general",
"country": "us",
"description": "We've officially lost count of how many times Rand Paul has claimed to be the victim of some sort of foiled assassination plot.",
"image": null,
"language": "en",
"published_at": "2021-06-07T15:39:05+00:00",
"source": "queerty",
"title": "Rand Paul claims his life is in danger (again), says “I don’t know what the world’s coming to!”",
"url": "https://www.queerty.com/rand-paul-claims-life-danger-says-dont-know-worlds-coming-20210607",
},
If the api does not provide any option for filtered or rather projected output like you wanted as 'authors', you need to do the post processing of the test variable by any kind of iteration method.
For example
let authors = test.map( x => x.author )