简体   繁体   English

Firebase:在前端将对象数组转换为真正的数组 - 奇怪的行为

[英]Firebase: Convert array of objects into a true array on the front end - weird behavior

I am using onSnapshot to get data from Firebase to my front end.我正在使用onSnapshot将数据从 Firebase 获取到我的前端。 I'm trying to convert one field on the document, which is an array of objects, into a true array on the front end.我正在尝试将文档上的一个字段(对象数组)转换为前端的真实数组。

I already know that Firebase stores this data as an array of objects with each object's key as the index of the would-be array: 0, 1, 2, ...我已经知道 Firebase 将此数据存储为对象数组,每个对象的键作为可能数组的索引:0、1、2、...

However, on the front end, for display purposes, I was hoping I could actually convert this into an array I can map over.然而,在前端,出于显示目的,我希望我可以将其实际转换成一个数组,我可以 map 结束。 The data is a userChat s document for a chat app.数据是聊天应用程序的userChat文档。 See below:见下文:

{
  "conversationid": "someConvoId",
  "lastMessageCreatedAt": {
    "nanoseconds": 0,
    "seconds": 1598882400,
  },
  "lastMessageFromId": "someId",
  "lastMessageFromName": "Guy",
  "lastMessageText": "Hey, what up?",
  "readByUser": false,
  "usersArr": [
    {
      "userAvatar": "https://randomuser.me/api/portraits/men/65.jpg",
      "userId": "someId",
      "userName": "Guy",
    },
    {
      "userAvatar": "https://randomuser.me/api/portraits/men/60.jpg",
      "userId": "someOtherId",
      "userName": "Gal",
    },
  ],
}

The chat app uses group chat, so I'm using the usersArray to determine how many people are in the chat as well as display a user avatar, user name, and a "+1" (or +2, etc.) on the main Messages screen.聊天应用程序使用群聊,所以我使用usersArray来确定聊天中有多少人,并显示用户头像、用户名和“+1”(或 +2 等)主消息屏幕。 In trying to convert the usersArray into a true array on the front end, I am getting unusual behavior.在尝试将usersArray转换为前端的真正数组时,我遇到了异常行为。 No matter what I try, methods that would work on either Objects or Arrays are rejected.无论我尝试什么,适用于ObjectsArrays的方法都会被拒绝。 Object.keys() only works for a console.log , but not when I try to assign the value to a variable. Object.keys()仅适用于console.log ,但在我尝试将值分配给变量时无效。

const actuallyAnObject = chat.usersArray;
let realArray = actuallyAnObject.map((item, i) => item); // throws an error
console.log(Object.keys(actuallyAnObject); // displays in the console correctly
let keys = Object.keys(actuallyAnObject); // throws an error
let array = [];
Object.keys(actuallyAnObject).forEach((key) => { // throws an error
  array.push({[key]: actuallyAnObject[key]});
});
console.log("actuallyAnObject['0'] : ", actuallyAnObject["0"]); // displays correctly
console.log("typeof actuallyAnObject : ", typeof actuallyAnObject); // object

How can I actually convert this into a useable array?我怎样才能真正将它转换成一个可用的数组?

Posting this as Community Wiki, as it's based in the comments.将其发布为社区 Wiki,因为它基于评论。

It seems that due to the fact that the data was manually added into Firebase, there was a difference between the name in the fields.好像是因为Firebase是手动添加数据的缘故,字段中的名字有区别。 While one was usersArray the other one was usersArr .一个是usersArray ,另一个是usersArr This mistake was causing an inconsisten behavior while trying to load the data and convert it into a real usable array.在尝试加载数据并将其转换为真正可用的数组时,这个错误导致了不一致的行为。

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

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