1

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.

1
  • Do you only want to extract the elements in the included-array that has type equals to ´'roster´? Commented Nov 22, 2018 at 10:56

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

4 Comments

Well, how do I get the contents of attributes, relationships ?
{ type: 'roster', id: '7b095048-6923-430a-ace8-5269b3148499', attributes: { stats: [Object], won: 'false', shardId: 'pc-ru' }, relationships: { team: [Object], participants: [Object] } } ] Here is the content I received after filtering
in the same way now filter new received data?
you could do something like arrayWithRoster.forEach(item => item.relationships) with your own logic
0

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);
  }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.