How to Fetch APIs and Print first element in response Array?
up vote
0
down vote
favorite
My React Native app fetch API data and I need to print the first index of response but it's not, and gets all of the "ozone" for example in all child of the parent Array and when I print val[0] when Mapping I have nothing printed
My Code|
export default class App extends Component {
constructor(props) {
super(props);
this.state = { isLoading: true, dataSource: null };
}
async componentDidMount() {
let API_WEATHER =
"https://api.weatherbit.io/v2.0/forecast/daily?city=Raleigh,NC&key={API_KEY}";
fetch(API_WEATHER)
.then(response => response.json())
.then(responseJson => {
console.log(responseJson.data);
this.setState({
isLoading: false,
dataSource: responseJson.data
});
})
.catch(error => {
console.log(error);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator size="large" />
</View>
);
}
let weather= this.state.dataSource.map((val, key) => {
return (
<Text key={key}>
{val.ozone}
</Text>
);
});
return (
<ScrollView style={styles.container}>
<ScrollView>
<View>
<Text>{weather}</Text>
</View>
</ScrollView>
</ScrollView>
);
}
In this part of the code when i log the respone JSON obj
.then(responseJson => {
console.log(responseJson.data);
console.log(responseJson.data[0]);
console.log(responseJson.data[0].datetime);
}
i have what i need, but when print them in View i have Erroe
look at the Images
javascript reactjs api react-native fetch
add a comment |
up vote
0
down vote
favorite
My React Native app fetch API data and I need to print the first index of response but it's not, and gets all of the "ozone" for example in all child of the parent Array and when I print val[0] when Mapping I have nothing printed
My Code|
export default class App extends Component {
constructor(props) {
super(props);
this.state = { isLoading: true, dataSource: null };
}
async componentDidMount() {
let API_WEATHER =
"https://api.weatherbit.io/v2.0/forecast/daily?city=Raleigh,NC&key={API_KEY}";
fetch(API_WEATHER)
.then(response => response.json())
.then(responseJson => {
console.log(responseJson.data);
this.setState({
isLoading: false,
dataSource: responseJson.data
});
})
.catch(error => {
console.log(error);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator size="large" />
</View>
);
}
let weather= this.state.dataSource.map((val, key) => {
return (
<Text key={key}>
{val.ozone}
</Text>
);
});
return (
<ScrollView style={styles.container}>
<ScrollView>
<View>
<Text>{weather}</Text>
</View>
</ScrollView>
</ScrollView>
);
}
In this part of the code when i log the respone JSON obj
.then(responseJson => {
console.log(responseJson.data);
console.log(responseJson.data[0]);
console.log(responseJson.data[0].datetime);
}
i have what i need, but when print them in View i have Erroe
look at the Images
javascript reactjs api react-native fetch
what is the output of ` console.log(responseJson.data);`?
– Akrion
Nov 10 at 19:13
@AkrionI have an Array of objects
– Gh a
Nov 10 at 19:16
@Akrion check the update issue
– Gh a
Nov 10 at 19:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My React Native app fetch API data and I need to print the first index of response but it's not, and gets all of the "ozone" for example in all child of the parent Array and when I print val[0] when Mapping I have nothing printed
My Code|
export default class App extends Component {
constructor(props) {
super(props);
this.state = { isLoading: true, dataSource: null };
}
async componentDidMount() {
let API_WEATHER =
"https://api.weatherbit.io/v2.0/forecast/daily?city=Raleigh,NC&key={API_KEY}";
fetch(API_WEATHER)
.then(response => response.json())
.then(responseJson => {
console.log(responseJson.data);
this.setState({
isLoading: false,
dataSource: responseJson.data
});
})
.catch(error => {
console.log(error);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator size="large" />
</View>
);
}
let weather= this.state.dataSource.map((val, key) => {
return (
<Text key={key}>
{val.ozone}
</Text>
);
});
return (
<ScrollView style={styles.container}>
<ScrollView>
<View>
<Text>{weather}</Text>
</View>
</ScrollView>
</ScrollView>
);
}
In this part of the code when i log the respone JSON obj
.then(responseJson => {
console.log(responseJson.data);
console.log(responseJson.data[0]);
console.log(responseJson.data[0].datetime);
}
i have what i need, but when print them in View i have Erroe
look at the Images
javascript reactjs api react-native fetch
My React Native app fetch API data and I need to print the first index of response but it's not, and gets all of the "ozone" for example in all child of the parent Array and when I print val[0] when Mapping I have nothing printed
My Code|
export default class App extends Component {
constructor(props) {
super(props);
this.state = { isLoading: true, dataSource: null };
}
async componentDidMount() {
let API_WEATHER =
"https://api.weatherbit.io/v2.0/forecast/daily?city=Raleigh,NC&key={API_KEY}";
fetch(API_WEATHER)
.then(response => response.json())
.then(responseJson => {
console.log(responseJson.data);
this.setState({
isLoading: false,
dataSource: responseJson.data
});
})
.catch(error => {
console.log(error);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator size="large" />
</View>
);
}
let weather= this.state.dataSource.map((val, key) => {
return (
<Text key={key}>
{val.ozone}
</Text>
);
});
return (
<ScrollView style={styles.container}>
<ScrollView>
<View>
<Text>{weather}</Text>
</View>
</ScrollView>
</ScrollView>
);
}
In this part of the code when i log the respone JSON obj
.then(responseJson => {
console.log(responseJson.data);
console.log(responseJson.data[0]);
console.log(responseJson.data[0].datetime);
}
i have what i need, but when print them in View i have Erroe
look at the Images
javascript reactjs api react-native fetch
javascript reactjs api react-native fetch
edited Nov 10 at 19:30
asked Nov 10 at 19:04
Gh a
707
707
what is the output of ` console.log(responseJson.data);`?
– Akrion
Nov 10 at 19:13
@AkrionI have an Array of objects
– Gh a
Nov 10 at 19:16
@Akrion check the update issue
– Gh a
Nov 10 at 19:35
add a comment |
what is the output of ` console.log(responseJson.data);`?
– Akrion
Nov 10 at 19:13
@AkrionI have an Array of objects
– Gh a
Nov 10 at 19:16
@Akrion check the update issue
– Gh a
Nov 10 at 19:35
what is the output of ` console.log(responseJson.data);`?
– Akrion
Nov 10 at 19:13
what is the output of ` console.log(responseJson.data);`?
– Akrion
Nov 10 at 19:13
@AkrionI have an Array of objects
– Gh a
Nov 10 at 19:16
@AkrionI have an Array of objects
– Gh a
Nov 10 at 19:16
@Akrion check the update issue
– Gh a
Nov 10 at 19:35
@Akrion check the update issue
– Gh a
Nov 10 at 19:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You're probably the first key of the object.
obj[Object.keys(obj)[0]];
Also, you can use
Try the for … in loop and break after the first iteration
for (var prop in object) {
// object[prop]
break;
}
Nope, I need to print object number One in the Hole Response Array which contains 16 objects then print the elements of this object num 1 look at last screenShots in Q
– Gh a
Nov 10 at 19:52
so you need an array of the obj keys and only after that it's possible to get the first by index
– victor zadorozhnyy
Nov 10 at 19:57
hmm,can you manipulate your code "obj[Object.keys(obj)[0]];" and put in my our Code ?
– Gh a
Nov 10 at 20:02
Oh, I do it :D, Thank you, bro !
– Gh a
Nov 10 at 20:05
@Gha Can you accept this answer?
– victor zadorozhnyy
Nov 10 at 20:08
|
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You're probably the first key of the object.
obj[Object.keys(obj)[0]];
Also, you can use
Try the for … in loop and break after the first iteration
for (var prop in object) {
// object[prop]
break;
}
Nope, I need to print object number One in the Hole Response Array which contains 16 objects then print the elements of this object num 1 look at last screenShots in Q
– Gh a
Nov 10 at 19:52
so you need an array of the obj keys and only after that it's possible to get the first by index
– victor zadorozhnyy
Nov 10 at 19:57
hmm,can you manipulate your code "obj[Object.keys(obj)[0]];" and put in my our Code ?
– Gh a
Nov 10 at 20:02
Oh, I do it :D, Thank you, bro !
– Gh a
Nov 10 at 20:05
@Gha Can you accept this answer?
– victor zadorozhnyy
Nov 10 at 20:08
|
show 3 more comments
up vote
0
down vote
accepted
You're probably the first key of the object.
obj[Object.keys(obj)[0]];
Also, you can use
Try the for … in loop and break after the first iteration
for (var prop in object) {
// object[prop]
break;
}
Nope, I need to print object number One in the Hole Response Array which contains 16 objects then print the elements of this object num 1 look at last screenShots in Q
– Gh a
Nov 10 at 19:52
so you need an array of the obj keys and only after that it's possible to get the first by index
– victor zadorozhnyy
Nov 10 at 19:57
hmm,can you manipulate your code "obj[Object.keys(obj)[0]];" and put in my our Code ?
– Gh a
Nov 10 at 20:02
Oh, I do it :D, Thank you, bro !
– Gh a
Nov 10 at 20:05
@Gha Can you accept this answer?
– victor zadorozhnyy
Nov 10 at 20:08
|
show 3 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You're probably the first key of the object.
obj[Object.keys(obj)[0]];
Also, you can use
Try the for … in loop and break after the first iteration
for (var prop in object) {
// object[prop]
break;
}
You're probably the first key of the object.
obj[Object.keys(obj)[0]];
Also, you can use
Try the for … in loop and break after the first iteration
for (var prop in object) {
// object[prop]
break;
}
edited Nov 10 at 20:07
answered Nov 10 at 19:39
victor zadorozhnyy
251412
251412
Nope, I need to print object number One in the Hole Response Array which contains 16 objects then print the elements of this object num 1 look at last screenShots in Q
– Gh a
Nov 10 at 19:52
so you need an array of the obj keys and only after that it's possible to get the first by index
– victor zadorozhnyy
Nov 10 at 19:57
hmm,can you manipulate your code "obj[Object.keys(obj)[0]];" and put in my our Code ?
– Gh a
Nov 10 at 20:02
Oh, I do it :D, Thank you, bro !
– Gh a
Nov 10 at 20:05
@Gha Can you accept this answer?
– victor zadorozhnyy
Nov 10 at 20:08
|
show 3 more comments
Nope, I need to print object number One in the Hole Response Array which contains 16 objects then print the elements of this object num 1 look at last screenShots in Q
– Gh a
Nov 10 at 19:52
so you need an array of the obj keys and only after that it's possible to get the first by index
– victor zadorozhnyy
Nov 10 at 19:57
hmm,can you manipulate your code "obj[Object.keys(obj)[0]];" and put in my our Code ?
– Gh a
Nov 10 at 20:02
Oh, I do it :D, Thank you, bro !
– Gh a
Nov 10 at 20:05
@Gha Can you accept this answer?
– victor zadorozhnyy
Nov 10 at 20:08
Nope, I need to print object number One in the Hole Response Array which contains 16 objects then print the elements of this object num 1 look at last screenShots in Q
– Gh a
Nov 10 at 19:52
Nope, I need to print object number One in the Hole Response Array which contains 16 objects then print the elements of this object num 1 look at last screenShots in Q
– Gh a
Nov 10 at 19:52
so you need an array of the obj keys and only after that it's possible to get the first by index
– victor zadorozhnyy
Nov 10 at 19:57
so you need an array of the obj keys and only after that it's possible to get the first by index
– victor zadorozhnyy
Nov 10 at 19:57
hmm,can you manipulate your code "obj[Object.keys(obj)[0]];" and put in my our Code ?
– Gh a
Nov 10 at 20:02
hmm,can you manipulate your code "obj[Object.keys(obj)[0]];" and put in my our Code ?
– Gh a
Nov 10 at 20:02
Oh, I do it :D, Thank you, bro !
– Gh a
Nov 10 at 20:05
Oh, I do it :D, Thank you, bro !
– Gh a
Nov 10 at 20:05
@Gha Can you accept this answer?
– victor zadorozhnyy
Nov 10 at 20:08
@Gha Can you accept this answer?
– victor zadorozhnyy
Nov 10 at 20:08
|
show 3 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242432%2fhow-to-fetch-apis-and-print-first-element-in-response-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what is the output of ` console.log(responseJson.data);`?
– Akrion
Nov 10 at 19:13
@AkrionI have an Array of objects
– Gh a
Nov 10 at 19:16
@Akrion check the update issue
– Gh a
Nov 10 at 19:35