0

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!

3
  • So basically you want to create a reference to the object outside of the array? const object = array[0] Commented Apr 26, 2021 at 16:30
  • @Other Me Yes exactly Commented Apr 26, 2021 at 16:32
  • Your expected output is not valid javascript. Commented Apr 26, 2021 at 16:59

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.