Reputation: 498
I have an a JSON response from an API service below
data = [{
id: 'event-1',
title: 'All Day Event',
start: '-01T14:30:00',
end: '-02T14:30:00',
className: "bg-danger",
description: 'Aenean fermentum quam vel sapien rutrum cursus. Vestibulum imperdiet finibus odio, nec tincidunt felis facilisis eu. '
},
{
id: 'event-2',
title: 'kiniko',
start: '-01T14:30:00',
end: '-02T14:30:00',
className: "bg-success",
description: 'Vestibulum imperdiet finibus odio, '
},
.....
];
How can i i convert it to only object or extract the object?
example is below
convertedObject = {
id: 'event-1',
title: 'All Day Event',
start: '-01T14:30:00',
end: '-02T14:30:00',
className: "bg-danger",
description: 'Aenean fermentum quam vel sapien rutrum cursus. Vestibulum imperdiet finibus odio, nec tincidunt felis facilisis eu. '
},
{
id: 'event-2',
title: 'kiniko',
start: '-01T14:30:00',
end: '-02T14:30:00',
className: "bg-success",
description: 'Vestibulum imperdiet finibus odio, '
}
....
;
In the convertedObject variable, the [] array should be gone leaving only the object {}.
Thanks guys!
Upvotes: 0
Views: 1627
Reputation: 508
The object is simply the first item in the array so you should be able to reference it with a simple [0]
:
const object = data[0];
Upvotes: 2
Reputation: 2412
You can simply access the 0th index of the returned result, which will help you in getting only the object.
let data = [{ ...your data }]
let obj = data[0]
Upvotes: 1