简体   繁体   English

如何将对象数组从 API 响应转换为 object

[英]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我有一个来自 API 服务的 JSON 响应

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? ii 如何将其仅转换为 object 或提取 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 {}.在 convertObject 变量中,[] 数组应该消失,只留下 object {}。

Thanks guys!多谢你们!

You can simply access the 0th index of the returned result, which will help you in getting only the object.您可以简单地访问返回结果的第 0 个索引,这将帮助您仅获取 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] : object 只是数组中的第一项,因此您应该能够使用简单的[0]来引用它:

const object = data[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将对象数组转换为指定的 object 格式? - How do I convert an array of objects to a specified object format? 如何在 JavaScript 中(动态地)将对象数组转换为对象 - How do I convert an array of objects into an object (dynamically) in JavaScript 如何将对象数组转换为 javascript 中的 object? - How do I convert array of objects to object in javascript? 如何在 JavaScript 中将对象数组转换为一个对象? - How do I convert array of Objects into one Object in JavaScript? 如何将对象数组转换为 JavaScript 中的一个 Object? - How do I convert array of Objects to one Object in JavaScript? 我如何从 rest API 响应数组 object 获得特定值? - How do i get particular value from rest API response array object? 如何从对象数组中提取对象(数组)? - How do I extract an object(array) from an array of objects? 如何将字典从 localStorage 转换为 Vue 中的对象数组? - How do I convert a dictionary from localStorage to an array of objects in Vue? 我如何将这种类型的 api 响应转换为对象数组。 在反应中 - How can i convert this type of api response to array of objects. in reactjs 如何从 object 获取数据并创建对象数组? - How do i get data from object and create array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM