api 正在返回一个带有编号和命名键的数组

api is returning an array with numbered and named keys

所以我正在整理一个 NextJS + Ghost CMS 解耦博客,并正在使用 Ghost JS API(特别是内容 API 客户端库)。

当我 运行 我的查询使用以下...

api.posts
    .browse({ 
      limit: 5,
      page: 1,
      include: 'authors',
      fields: 'excerpt,custom_excerpt,title,id,slug,'
    })  
    .then((posts) => {
      console.log(posts);
        return posts;
      })
    .catch((err) => {
        console.error(err);
    });

...我发现它返回一个数组。该数组具有典型的 0 1 2 ... 键。我惊讶地发现它还有一个命名的最后一项:meta

查看来自 chrome JS 控制台的图像。

这是怎么回事。我是否错过了 JS101 中关于数组 string-named 键的课程?

来自MDN:

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.

也就是说,它们是一种特殊类型的物体。因此,与其他 objects 一样,您可以向其添加 named properties。然而,他的 prototype 上可用的 methods 仅遍历 numeric-properties。检查下一个示例:

let arr = [1,2,3,4];
arr.meta = {foo: "bar"};
console.log("standard arr display: ", arr);
console.log("arr.meta: ", arr.meta);
console.log("Own Property Names: ", Object.getOwnPropertyNames(arr));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

现在,我相信 chrome JS 控制台 的实现是基于 object 原型方法,也许 Object.getOwnPropertNames(), to display all his properties (including the non-enumerable length) and values. You can give a read to Object.defineProperty()查看如何使用 descriptor 参数定义 non-enumerable 属性。