Javascript gets undefined on array
up vote
-1
down vote
favorite
I have the following data structure for javascript:
var data = {
"announcements": {
"IFT4S": [{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
}, {
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
}, {
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
}]
}
}
I want to access the values of the keys "read_state", "posted_at", "title" and "message".
However, when I try data.announcements.IFT4S["title"]
or any other key instead of "title" I get the undefined in the console.
What am I doing wrong?
javascript
add a comment |
up vote
-1
down vote
favorite
I have the following data structure for javascript:
var data = {
"announcements": {
"IFT4S": [{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
}, {
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
}, {
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
}]
}
}
I want to access the values of the keys "read_state", "posted_at", "title" and "message".
However, when I try data.announcements.IFT4S["title"]
or any other key instead of "title" I get the undefined in the console.
What am I doing wrong?
javascript
4
IFT4S
is an array. You will need to iterate over it to access objects. e.g.data.announcements.IFT4S[0]["title"]
– Nikhil Aggarwal
Nov 8 at 16:47
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have the following data structure for javascript:
var data = {
"announcements": {
"IFT4S": [{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
}, {
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
}, {
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
}]
}
}
I want to access the values of the keys "read_state", "posted_at", "title" and "message".
However, when I try data.announcements.IFT4S["title"]
or any other key instead of "title" I get the undefined in the console.
What am I doing wrong?
javascript
I have the following data structure for javascript:
var data = {
"announcements": {
"IFT4S": [{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
}, {
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
}, {
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
}]
}
}
I want to access the values of the keys "read_state", "posted_at", "title" and "message".
However, when I try data.announcements.IFT4S["title"]
or any other key instead of "title" I get the undefined in the console.
What am I doing wrong?
javascript
javascript
edited Nov 8 at 16:50
Amy
21.1k1772131
21.1k1772131
asked Nov 8 at 16:46
Roman
31
31
4
IFT4S
is an array. You will need to iterate over it to access objects. e.g.data.announcements.IFT4S[0]["title"]
– Nikhil Aggarwal
Nov 8 at 16:47
add a comment |
4
IFT4S
is an array. You will need to iterate over it to access objects. e.g.data.announcements.IFT4S[0]["title"]
– Nikhil Aggarwal
Nov 8 at 16:47
4
4
IFT4S
is an array. You will need to iterate over it to access objects. e.g. data.announcements.IFT4S[0]["title"]
– Nikhil Aggarwal
Nov 8 at 16:47
IFT4S
is an array. You will need to iterate over it to access objects. e.g. data.announcements.IFT4S[0]["title"]
– Nikhil Aggarwal
Nov 8 at 16:47
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
IFT4S is an array, you can access its objects and their values values by calling:
data.announcements.IFT4S[index].title
with index beeing one of 0-2 here since the array contains 3 objects.
For example:
data.announcements.IFT4S[0].title
This is a very basic concept, check out any javascript guide to learn about arrays.
add a comment |
up vote
1
down vote
when I try data.announcements.IFT4S["title"] or any other key instead
of "title" I get the undefined in the console. What am I doing wrong?
What you are doing here is trying to access the title key of the IFT4S array.
The issue is that IFT4S doesn't have a title key. Instead, like an array object, it has indexes as keys.
IFT4S = [ {...}, {...}, {...} ]
To access the first element of the IFT4S array you would do it like this
IFT4S[0]
In your case that would return the object at the first position of IFT4S array (index 0)
{
id: "D7214",
read_state: "unread",
posted_at: "2018-10-25T14:35:54Z",
title: "Reminder! Systems disruption: 27-28 Oct",
message: "All University online systems will be unavailable."
}
If you want to get all the titles from all the elements inside IFT4S array you could do this
IFT4S.map(element => element.title)
Array.prototype.map returns a new array where each element is the result of applying the function specified inside map to each element of the original array.
In this case, it would return
[
"Reminder! Systems disruption: 27-28 Oct",
"Stem Fair",
"Smile more, worry less with our FREE course"
]
add a comment |
up vote
1
down vote
You have to itrate over the array to get value from an array of object
var data = {
"announcements": {
"IFT4S": [
{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
},
{
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
},
{
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
},
]
}
}
data.announcements.IFT4S.forEach(item => {
console.log(item.title)
})
or you can do like this,
0
is the index
console.log(data.announcements.IFT4S[0].read_state)
console.log(data.announcements.IFT4S[0].title)
Nice. Might I suggest using item.title in your working example. The secondary example is not connected properly so it's broken. Still really good.
– mcv
Nov 8 at 18:51
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
IFT4S is an array, you can access its objects and their values values by calling:
data.announcements.IFT4S[index].title
with index beeing one of 0-2 here since the array contains 3 objects.
For example:
data.announcements.IFT4S[0].title
This is a very basic concept, check out any javascript guide to learn about arrays.
add a comment |
up vote
0
down vote
accepted
IFT4S is an array, you can access its objects and their values values by calling:
data.announcements.IFT4S[index].title
with index beeing one of 0-2 here since the array contains 3 objects.
For example:
data.announcements.IFT4S[0].title
This is a very basic concept, check out any javascript guide to learn about arrays.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
IFT4S is an array, you can access its objects and their values values by calling:
data.announcements.IFT4S[index].title
with index beeing one of 0-2 here since the array contains 3 objects.
For example:
data.announcements.IFT4S[0].title
This is a very basic concept, check out any javascript guide to learn about arrays.
IFT4S is an array, you can access its objects and their values values by calling:
data.announcements.IFT4S[index].title
with index beeing one of 0-2 here since the array contains 3 objects.
For example:
data.announcements.IFT4S[0].title
This is a very basic concept, check out any javascript guide to learn about arrays.
answered Nov 8 at 16:52
Evoxx
465
465
add a comment |
add a comment |
up vote
1
down vote
when I try data.announcements.IFT4S["title"] or any other key instead
of "title" I get the undefined in the console. What am I doing wrong?
What you are doing here is trying to access the title key of the IFT4S array.
The issue is that IFT4S doesn't have a title key. Instead, like an array object, it has indexes as keys.
IFT4S = [ {...}, {...}, {...} ]
To access the first element of the IFT4S array you would do it like this
IFT4S[0]
In your case that would return the object at the first position of IFT4S array (index 0)
{
id: "D7214",
read_state: "unread",
posted_at: "2018-10-25T14:35:54Z",
title: "Reminder! Systems disruption: 27-28 Oct",
message: "All University online systems will be unavailable."
}
If you want to get all the titles from all the elements inside IFT4S array you could do this
IFT4S.map(element => element.title)
Array.prototype.map returns a new array where each element is the result of applying the function specified inside map to each element of the original array.
In this case, it would return
[
"Reminder! Systems disruption: 27-28 Oct",
"Stem Fair",
"Smile more, worry less with our FREE course"
]
add a comment |
up vote
1
down vote
when I try data.announcements.IFT4S["title"] or any other key instead
of "title" I get the undefined in the console. What am I doing wrong?
What you are doing here is trying to access the title key of the IFT4S array.
The issue is that IFT4S doesn't have a title key. Instead, like an array object, it has indexes as keys.
IFT4S = [ {...}, {...}, {...} ]
To access the first element of the IFT4S array you would do it like this
IFT4S[0]
In your case that would return the object at the first position of IFT4S array (index 0)
{
id: "D7214",
read_state: "unread",
posted_at: "2018-10-25T14:35:54Z",
title: "Reminder! Systems disruption: 27-28 Oct",
message: "All University online systems will be unavailable."
}
If you want to get all the titles from all the elements inside IFT4S array you could do this
IFT4S.map(element => element.title)
Array.prototype.map returns a new array where each element is the result of applying the function specified inside map to each element of the original array.
In this case, it would return
[
"Reminder! Systems disruption: 27-28 Oct",
"Stem Fair",
"Smile more, worry less with our FREE course"
]
add a comment |
up vote
1
down vote
up vote
1
down vote
when I try data.announcements.IFT4S["title"] or any other key instead
of "title" I get the undefined in the console. What am I doing wrong?
What you are doing here is trying to access the title key of the IFT4S array.
The issue is that IFT4S doesn't have a title key. Instead, like an array object, it has indexes as keys.
IFT4S = [ {...}, {...}, {...} ]
To access the first element of the IFT4S array you would do it like this
IFT4S[0]
In your case that would return the object at the first position of IFT4S array (index 0)
{
id: "D7214",
read_state: "unread",
posted_at: "2018-10-25T14:35:54Z",
title: "Reminder! Systems disruption: 27-28 Oct",
message: "All University online systems will be unavailable."
}
If you want to get all the titles from all the elements inside IFT4S array you could do this
IFT4S.map(element => element.title)
Array.prototype.map returns a new array where each element is the result of applying the function specified inside map to each element of the original array.
In this case, it would return
[
"Reminder! Systems disruption: 27-28 Oct",
"Stem Fair",
"Smile more, worry less with our FREE course"
]
when I try data.announcements.IFT4S["title"] or any other key instead
of "title" I get the undefined in the console. What am I doing wrong?
What you are doing here is trying to access the title key of the IFT4S array.
The issue is that IFT4S doesn't have a title key. Instead, like an array object, it has indexes as keys.
IFT4S = [ {...}, {...}, {...} ]
To access the first element of the IFT4S array you would do it like this
IFT4S[0]
In your case that would return the object at the first position of IFT4S array (index 0)
{
id: "D7214",
read_state: "unread",
posted_at: "2018-10-25T14:35:54Z",
title: "Reminder! Systems disruption: 27-28 Oct",
message: "All University online systems will be unavailable."
}
If you want to get all the titles from all the elements inside IFT4S array you could do this
IFT4S.map(element => element.title)
Array.prototype.map returns a new array where each element is the result of applying the function specified inside map to each element of the original array.
In this case, it would return
[
"Reminder! Systems disruption: 27-28 Oct",
"Stem Fair",
"Smile more, worry less with our FREE course"
]
answered Nov 8 at 17:15
Joaquin Timerman
1245
1245
add a comment |
add a comment |
up vote
1
down vote
You have to itrate over the array to get value from an array of object
var data = {
"announcements": {
"IFT4S": [
{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
},
{
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
},
{
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
},
]
}
}
data.announcements.IFT4S.forEach(item => {
console.log(item.title)
})
or you can do like this,
0
is the index
console.log(data.announcements.IFT4S[0].read_state)
console.log(data.announcements.IFT4S[0].title)
Nice. Might I suggest using item.title in your working example. The secondary example is not connected properly so it's broken. Still really good.
– mcv
Nov 8 at 18:51
add a comment |
up vote
1
down vote
You have to itrate over the array to get value from an array of object
var data = {
"announcements": {
"IFT4S": [
{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
},
{
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
},
{
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
},
]
}
}
data.announcements.IFT4S.forEach(item => {
console.log(item.title)
})
or you can do like this,
0
is the index
console.log(data.announcements.IFT4S[0].read_state)
console.log(data.announcements.IFT4S[0].title)
Nice. Might I suggest using item.title in your working example. The secondary example is not connected properly so it's broken. Still really good.
– mcv
Nov 8 at 18:51
add a comment |
up vote
1
down vote
up vote
1
down vote
You have to itrate over the array to get value from an array of object
var data = {
"announcements": {
"IFT4S": [
{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
},
{
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
},
{
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
},
]
}
}
data.announcements.IFT4S.forEach(item => {
console.log(item.title)
})
or you can do like this,
0
is the index
console.log(data.announcements.IFT4S[0].read_state)
console.log(data.announcements.IFT4S[0].title)
You have to itrate over the array to get value from an array of object
var data = {
"announcements": {
"IFT4S": [
{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
},
{
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
},
{
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
},
]
}
}
data.announcements.IFT4S.forEach(item => {
console.log(item.title)
})
or you can do like this,
0
is the index
console.log(data.announcements.IFT4S[0].read_state)
console.log(data.announcements.IFT4S[0].title)
var data = {
"announcements": {
"IFT4S": [
{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
},
{
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
},
{
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
},
]
}
}
data.announcements.IFT4S.forEach(item => {
console.log(item.title)
})
var data = {
"announcements": {
"IFT4S": [
{
"id": "D7214",
"read_state": "unread",
"posted_at": "2018-10-25T14:35:54Z",
"title": "Reminder! Systems disruption: 27-28 Oct",
"message": "All University online systems will be unavailable."
},
{
"id": "B399C",
"read_state": "read",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Stem Fair",
"message": "The STEM Careers Fair is taking place on 31 October 2018"
},
{
"id": "6F5EE",
"read_state": "unread",
"posted_at": "2018-10-22T09:04:48Z",
"title": "Smile more, worry less with our FREE course",
"message": "Take part in our Online Mindfulness Programme."
},
]
}
}
data.announcements.IFT4S.forEach(item => {
console.log(item.title)
})
console.log(data.announcements.IFT4S[0].read_state)
console.log(data.announcements.IFT4S[0].title)
console.log(data.announcements.IFT4S[0].read_state)
console.log(data.announcements.IFT4S[0].title)
edited Nov 9 at 0:32
answered Nov 8 at 16:53
Hafeez Hamza
307110
307110
Nice. Might I suggest using item.title in your working example. The secondary example is not connected properly so it's broken. Still really good.
– mcv
Nov 8 at 18:51
add a comment |
Nice. Might I suggest using item.title in your working example. The secondary example is not connected properly so it's broken. Still really good.
– mcv
Nov 8 at 18:51
Nice. Might I suggest using item.title in your working example. The secondary example is not connected properly so it's broken. Still really good.
– mcv
Nov 8 at 18:51
Nice. Might I suggest using item.title in your working example. The secondary example is not connected properly so it's broken. Still really good.
– mcv
Nov 8 at 18:51
add a comment |
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%2f53212381%2fjavascript-gets-undefined-on-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
4
IFT4S
is an array. You will need to iterate over it to access objects. e.g.data.announcements.IFT4S[0]["title"]
– Nikhil Aggarwal
Nov 8 at 16:47