Reputation: 201
When parsing I get this answer:
{
data: {
type: 'match',
id: 'ae825686-8a74-4363-a1d1-402d5a4207d5',
attributes: {
createdAt: '2018-11-20T17:06:52Z',
titleId: 'bluehole-pubg',
shardId: 'pc-ru',
tags: null,
seasonState: 'progress',
duration: 1823,
stats: null,
gameMode: 'squad-fpp'
},
},
included: [
{
type: 'participant',
id: '0b1b8f78-bb3e-4c0a-9955-9fdf8e33e5b4',
attributes: [Object]
},
{
type: 'participant',
id: '85e74b88-125b-4529-8c3f-fd76bd43b9aa',
attributes: [Object]
},
{
type: 'roster',
id: '6db70dce-b056-4bed-8cc4-6521b83bea50',
attributes: [Object],
relationships: [Object]
},
{
type: 'roster',
id: 'a35db9ae-e559-4474-b922-784e3221f484',
attributes: [Object],
relationships: [Object]
}
]
}
I need to get data with inculded type:'roster', and object contents attributes, relationships. How can I do it? I tried extracting array of data console.log(included [0]);
I get data from type:'participant'. Tried so console.log (included [{type: 'roster', relationship}]);
In response, I get the message undefined Please tell me how to get the necessary data.
Upvotes: 1
Views: 111
Reputation: 962
I would create two arrays and push individually, like this:
let json = {
data: {
type: 'match',
id: 'ae825686-8a74-4363-a1d1-402d5a4207d5',
attributes: {
createdAt: '2018-11-20T17:06:52Z',
titleId: 'bluehole-pubg',
shardId: 'pc-ru',
tags: null,
seasonState: 'progress',
duration: 1823,
stats: null,
gameMode: 'squad-fpp'
},
},
included: [
{
type: 'participant',
id: '0b1b8f78-bb3e-4c0a-9955-9fdf8e33e5b4',
attributes: [Object]
},
{
type: 'participant',
id: '85e74b88-125b-4529-8c3f-fd76bd43b9aa',
attributes: [Object]
},
{
type: 'roster',
id: '6db70dce-b056-4bed-8cc4-6521b83bea50',
attributes: [Object],
relationships: [Object]
},
{
type: 'roster',
id: 'a35db9ae-e559-4474-b922-784e3221f484',
attributes: [Object],
relationships: [Object]
}
]
}
let attributes = [];
let relationships = [];
let dataIneed = json.included.forEach(element => {
if(element.type === 'roster'){
attributes.push(element.attributes);
relationships.push(element.relationships);
}
})
Upvotes: 0
Reputation: 1894
Using Array.prototype.filter()
const response = {
data: {
type: 'match',
id: 'ae825686-8a74-4363-a1d1-402d5a4207d5',
attributes: {
createdAt: '2018-11-20T17:06:52Z',
titleId: 'bluehole-pubg',
shardId: 'pc-ru',
tags: null,
seasonState: 'progress',
duration: 1823,
stats: null,
gameMode: 'squad-fpp'
},
},
included: [
{
type: 'participant',
id: '0b1b8f78-bb3e-4c0a-9955-9fdf8e33e5b4',
attributes: [Object]
},
{
type: 'participant',
id: '85e74b88-125b-4529-8c3f-fd76bd43b9aa',
attributes: [Object]
},
{
type: 'roster',
id: '6db70dce-b056-4bed-8cc4-6521b83bea50',
attributes: [Object],
relationships: [Object]
},
{
type: 'roster',
id: 'a35db9ae-e559-4474-b922-784e3221f484',
attributes: [Object],
relationships: [Object]
}
]
}
const arrayWithRoster = response.included.filter(item => item.type === 'roster');
console.log(arrayWithRoster);
Upvotes: 2