[英]Why do I commonly see data inside “container” datatypes like an object inside and array inside an object?
I have dangerously little actual programming experience, but when I delve into the data of actual coders I often encounter something like this and I don't understand why...我几乎没有实际的编程经验,但是当我深入研究实际编码人员的数据时,我经常遇到这样的事情,我不明白为什么......
(from a little ditty I was working on in javascript this weekend) (这个周末我在 javascript 工作的小曲子)
{
"data": [
{
"name": "foo",
"id": 0
},
{
"name": "bar",
"id": 1
},
{
"name": "baz",
"id": 2
},
]
};
That's an object with a single key:value where the key is "data" and the value is an array, and the array is a list of objects inside each is a name and an ID.那是一个 object ,它有一个键:值,其中键是“数据”,值是一个数组,数组是一个对象列表,其中每个对象是一个名称和一个 ID。
It seems to me there's at least one extra layer of abstraction inside here.在我看来,这里至少有一个额外的抽象层。 Why not have Just the array of object?
为什么没有 object 的数组呢? Why have the outer object with one element?
为什么外部 object 有一个元素? I figure there's got to be a good reason, but it seems like it just makes it more difficult to access the data within.
我认为这一定是有充分理由的,但似乎它只是让访问其中的数据变得更加困难。
Say one of your coders implemented the endpoint GET /users
which returned an array of users假设您的一位编码人员实现了端点
GET /users
,该端点返回了一组用户
[
{ id: 0, name: 'foo' },
{ id: 1, name: 'bar' },
{ id: 2, name: 'baz' },
] // <- the response body of GET /users
This is good for a while.这在一段时间内是好的。 Then, your userbase starts getting large, and you realize you have to start paginating the endpoint with
limit
and offset
to avoid overloading clients.然后,您的用户群开始变大,并且您意识到必须开始使用
limit
和offset
对端点进行分页以避免客户端过载。 How would you track the last offset
?您将如何跟踪最后的
offset
?
You'll typically find response payloads like the one in your example to allow for this:您通常会找到类似于示例中的响应有效负载以允许这样做:
{
"data": [
{
"name": "foo",
"id": 0
},
{
"name": "bar",
"id": 1
},
{
"name": "baz",
"id": 2
},
],
offset: 200
};
You could then scale your userbase without worry.然后,您可以毫无顾虑地扩展您的用户群。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.