Reputation: 923
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
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