简体   繁体   中英

How do I convert array of Objects from an API Response into an object

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 ii 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!

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM