Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6213

create an object using forEach

$
0
0

Depending on the data returned by a graphql query, I want to create an object like this:

const DATA = [    {        id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba',        imageUrl: defaultUrl,        name: 'Johann',    },    {        id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',        imageUrl: defaultUrl,        name: 'Lars',    },    {        id: '58694a0f-3da1-471f-bd96-145571e29d72',        imageUrl: defaultUrl,        name: 'Sarah',    },];

However, I don't understand how I can iterate through the data and create an object out of it. For instance, by doing this, I can extract all fristnames into an array:

var friendNames = new Array();if (data !== null && data !== undefined) {    data.users.nodes[0].userRelations.forEach((relation: UserRelation) => {        friendNames.push(relation.relatedUser.firstName);    });}

However, apart from the first names, I also want to add the ids and a hardcoded link (which will be same for all items for now). But how can I get my data in a shape similar to const DATA


Viewing all articles
Browse latest Browse all 6213

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>