I am doing an AJAX request to a JSON and getting following code as response:
{
total: "1",
items: [
{
id: 43,
title: "ThisIsTheTitle",
promoted: false,
sticky: false,
weight: 10,
created: {
timestamp: 1482054,
formatted: "17/01/2017"
},
url: "http://...",
airdate: {
timestamp: 1484980,
formatted: "17/01/2017"
},
video: {
id: 43,
number_of_views: 1,
duration: {
seconds: 50,
formatted: "00:00"
},
required_login: false
},
program: {
id: 25,
url: "http://...",
title: "ProgrammaTitel"
},
image: {
uri: "public://...",
full: "http://..."
},
tags: [
{
id: "4",
name: "Map"
},
{
id: "7",
name: "Aflevering2"
}
]
}
]
}
Now I push this data into my own JSArray. Note there is now only 1 response-item but more will be added.
I want to retrieve specific object-data based on the name of a tag of the object (item > tags > name = 'Aflevering2')
So I would like the data from the object where the tag name is 'Aflevering2'.
Any advice? Thanks!
-
Looks like you are able to get the data (because you have shown it), but lets see what you have tried so far to access the element in question. Show your code (even with errors) please.Tigger– Tigger2017年01月17日 10:03:10 +00:00Commented Jan 17, 2017 at 10:03
1 Answer 1
You can find the items with a combination of filter() and some():
obj.items.filter(v => v.tags.some(k => k.name === 'Aflevering2'));
let obj = {
total: "1",
items: [
{
id: 43,
title: "ThisIsTheTitle",
promoted: false,
sticky: false,
weight: 10,
created: {
timestamp: 1482054,
formatted: "17/01/2017"
},
url: "http://...",
airdate: {
timestamp: 1484980,
formatted: "17/01/2017"
},
video: {
id: 43,
number_of_views: 1,
duration: {
seconds: 50,
formatted: "00:00"
},
required_login: false
},
program: {
id: 25,
url: "http://...",
title: "ProgrammaTitel"
},
image: {
uri: "public://...",
full: "http://..."
},
tags: [
{
id: "4",
name: "Map"
},
{
id: "7",
name: "Aflevering2"
}
]
}
]
}
let items = obj.items.filter(v => v.tags.some(k => k.name === 'Aflevering2'));
console.log(items);
answered Jan 17, 2017 at 10:10
baao
73.6k18 gold badges153 silver badges209 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
NickVH
I assume
v => v.tags.some(k => k.name === 'Aflevering2') is written shortened? How can I translate this code to 'full-length'? because I am getting this into my warnings: arrow function syntax (=>)' is only available in ES6 (use esnext option) and I'd like to understand the code I write :)baao
@Kemagezien it's an arrow function, it's not shortened. Google for arrow function js and the first result will explain it in deep.
NickVH
thanks for information I did some research to arrow-functions. However e.g. Safari does not support arrow-functions. Is it possible to re-write this without an arrow-function?
Explore related questions
See similar questions with these tags.
lang-js