简体   繁体   English

为什么我通常会在“容器”数据类型中看到数据,例如在 object 中看到数据,在 object 中看到数组?

[英]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.然后,您的用户群开始变大,并且您意识到必须开始使用limitoffset对端点进行分页以避免客户端过载。 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.

相关问题 为什么我在输入中看到[object Object]? - Why do i see [object Object] inside my input? 如何在数组内部的数组中搜索 object? - How do I search the object inside the array inside the array? 我如何存储数据使用此模式-对象内部对象在对象内部或对象内部对象在数组内部 - How i can store data use this pattern - Object inside object inside object or object inside object inside array 如何访问对象内部的数据? - how do I access data inside object? 如何访问位于数组内部,位于数组内部的对象的属性? - How do I access properties of an object that's inside of an array that's inside of an array that's inside of an array? 如何访问 React 容器内的样式对象? - How do I access styles object inside React container? 如何在JavaScript数组中按对象查找对象 - How do I find an object by object inside a javascript array 如何在这样的javascript对象中设置字段? - How do I set fields inside a javascript object like this one? 我如何将 object 推入 object 内的数组,该数组也在另一个数组内 - How do i push an object into an array which is inside an object which is also inside an another array 如何在对象数组 Javascript 中过滤数组 object? - How do I filter an array object inside of a array of objects Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM