COdek
COdek

Reputation: 923

How in app to GET some specific data from JSON-server

Please help me to get only what i want from server. I don't understand how this do. SERVER side:

{ 
'data': [ 
{ 'count': 'test', 
'title': 'test', 
'size': 2, 'id': 1 
}, 
{ 'count': 'test', 
'title': 'test', 
'size': 2, 'id': 2 
}, 
{ 'count': 'test', 
'title': 'test', 
'size': 2, 
'id': 3 
} 

And my actions from APP side:

fetch(url)
.then(response => response.json())
.then(res => { 
this.setState({ data: res}, () => {console.log('data: ', res)})})

data: Array [
Object { 
{ 
'count': 'test',
'title': 'test',
'size': 2,
'id': 1 
}, 
{
'count': 'test',
'title': 'test',
'size': 2,
'id': 2
},
{
'count': 'test',
'title': 'test',
'size': 2,
'id': 3
}
}
]

But, i want to GET only 'count' and 'title'.

Upvotes: 1

Views: 463

Answers (1)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48437

I want to get only 'count' and 'title'.

Then use map method and destructing. The map() method creates a new array with the results of calling a provided function on every element in the calling array.

let data = [ { 'count': 'test', 'title': 'test', 'size': 2, 'id': 1 }, { 'count': 'test', 'title': 'test', 'size': 2, 'id': 2 }, { 'count': 'test', 'title': 'test', 'size': 2, 'id': 3 } ]

console.log(data.map(({count, title}) => ({count, title})));

Upvotes: 2

Related Questions