简体   繁体   English

创建数组并将对象推送给它后,对象是 [Object] 而不是实际数据

[英]After creating an array and pushing objects to it, objects are [Object] and not the actual data

In node, I am trying to mutate an array of objects from S3 (super annoying response to parse) into something more front end useable.在节点中,我试图将 S3 中的一组对象(对解析的超级烦人的响应)转变为更前端可用的东西。 The initial object format is:初始 object 格式为:

[
  {
    Key: 'some/folder/structure/file.jpg',
    LastModified: 2020-05-14T02:16:59.000Z,
    ETag: '"SomeETag"',
    Size: 83630,
    StorageClass: 'STANDARD'
  }
]

I am trying to mutate this into an array like:我正在尝试将其变异为一个数组,例如:

name: "some"
files: [
  {

    key: 'some/folder/structure/file.jpg',
    name: 'file.jpg',
    folder: 'folder',
    subfolder: 'structure'

  }
]

key is the key I need to retrieve the object from s3. key 是我需要从 s3 检索 object 的密钥。 name is the name of file for front end display. name 是前端显示的文件名。 folder ends up being the top level folder I want to display the file in. Subfolder is anything between folder and the file name.文件夹最终成为我要在其中显示文件的顶级文件夹。子文件夹是文件夹和文件名之间的任何内容。

This processing will make an object for each actual top level folder from s3 ('some' in the example).此处理将为来自 s3 的每个实际顶级文件夹(示例中的“一些”)生成 object。 this object will have a name, and a list of file objects like the one above.这个 object 将有一个名称,以及一个类似上面的文件对象列表。

Here is my function to do such:这是我的 function 这样做:

function processResponse(res) {
  var result = [];
  res.forEach(item =>{
    if(item.Key.slice(-1) === '/') return;
    const split = item.Key.split('/');
    var obj = {
      key: item.Key,
      name: split[split.length-1],
      folder: split[1],
      subfolder: split.length <= 3 ? '' : split.join([2,split.length-2])
    }
    var index = result.findIndex(x => x.name === split[1])
    if(index === -1) {
      result = [...result, {
        name: split[1],
        files: []
      }]
      index = result.length-1
    }
    result[index].files = [...result[index].files, obj];
  });
  console.log(result)
  return result;
}

The issue I am seeing is with the files array for any individual folder.我看到的问题是任何单个文件夹的文件数组。 When I console log the resulting array, instead of displaying the data for each file, the return looks like this:当我控制台记录结果数组时,不是显示每个文件的数据,而是返回如下所示:

[
  {
    name: 'Forms',
    files: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ]
  }
]

Besides the data being missing, it also seems to break the return to front end.除了数据丢失外,似乎也打破了对前端的回归。 How can I push the actual data into each file array so it can be console.log-ed and returned correctly?如何将实际数据推送到每个文件数组中,以便它可以被 console.log-ed 并正确返回?

Bonus: I think the way I make the subfolder string is wrong.奖励:我认为我制作子文件夹字符串的方式是错误的。 Basically I want anything from split[2] to the last folder before the file or '' if split only has a length of 3.基本上我想要从 split[2] 到文件之前的最后一个文件夹的任何内容,或者如果 split 的长度仅为 3,则为 ''。

Thank you for the help!感谢您的帮助!

As it turns out, this works fine and my issue was not understanding how the console obscures deep data structured.事实证明,这很好用,我的问题是不了解控制台如何掩盖深层数据结构。 To view the full data, all I needed to do was:要查看完整数据,我需要做的就是:

console.log(JSON.stringify(result,0,2))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM