简体   繁体   English

传递关联数组作为道具不起作用

[英]Passing associative array as props not working

I have a React application which handles rooms and their statistics. 我有一个React应用程序来处理房间及其统计数据。

Previously, I had the code set up to pass as props to the next component: 以前,我将代码设置为作为道具传递给下一个组件:

  • the raw statistics (not a concern for the question) 原始统计数据(不是问题的关注点)
  • an array of all the rooms set up as follows 所有房间的阵列设置如下

    使用数字索引启动数组

I figured it would be simpler for me, though, to have the list of all rooms as an associative array where the keys of each element is the same as the ID it contains. 我认为,对于我来说,将所有房间的列表作为关联数组会更简单,其中每个元素的键与它包含的ID相同。 To do that, I utilized a code similar to this in a for loop: 为此,我在for循环中使用了与此类似的代码:

roomsList[rooms[v].ID] = rooms[v];

So that the result would be: 结果将是:

[a001: {...}, a002: {...}, ...]

I then proceeded to pass this style of array, and not the standard one with a numeric index, as a prop to the next component as such: 然后我继续传递这种样式的数组,而不是带有数字索引的标准数组,作为下一个组件的支柱:

<StatsBreakdown stats={computedStats.current} roomsList={roomsList} />

BUT

Now, the next component sees that prop as an empty array. 现在,下一个组件将prop视为空数组。

空数组的示例 Even more weirdly, if I initialize that roomsList array with a random value [0] and then do the same process, I end up with: 更奇怪的是,如果我使用随机值[0]初始化roomsList数组然后执行相同的过程,我最终得到: 使用随机数初始化的数组示例

I cannot cycle through the array with .map, and, according to JS, the length is actually 0, it's not only Google Chrome. 我无法使用.map循环遍历数组,而且据JS说,长度实际上是0,它不仅仅是Google Chrome。

Is there something I'm missing about the way JSX, JS or React work? 关于JSX,JS或React的工作方式,我有什么遗漏吗?

Your original roomsList was an array of objects , whose indices were 0,1,2 etc. roomsList[rooms[v].ID] = rooms[v]; 你原来的roomsList是一个对象数组 ,其索引是0,1,2等。 roomsList[rooms[v].ID] = rooms[v]; implies you are inserting elements not using a number but an alphanumeric string. 暗示您插入的元素不是使用数字而是使用字母数字字符串。 Hence your resulting array is no longer an array but an object . 因此,生成的数组不再是数组而是对象

So we can cycle over the object using Object.keys() . 所以我们可以使用Object.keys()遍历对象。

const renderRoomDets = Object.keys(roomsList).map(room => {
  roomOwner = roomsList[room].owner_id;
  return (
    <div>
      <p>{`Room Owner ${roomOwner}`}</p>
    </div>
  );
});

But I believe your original form is ideal, because you are reaping no special benefits from this notation. 但我相信你的原始形式是理想的,因为你从这种表示法中获得的收益并没有特别的好处。

A better alternative maybe using .find() or .findIndex() if you want iterate over an array based on a specific property. 如果您希望基于特定属性迭代数组,则可以使用.find().findIndex()

const matchedRoom = roomsList.find(room => room.ID === 'Srf4323')

Iterate the new array using its keys not indexes. 使用其键而非索引迭代新数组。

Or even better store your data in an actual object instead of an array since you're using strings for ids. 或者甚至更好地将数据存储在实际对象而不是数组中,因为您正在使用字符串表示ID。

First define your object like so: 首先像这样定义你的对象:

let data = {};

Then start adding records to it. 然后开始向其添加记录。 I'd suggest deleting the ID attribute of the object since you're storing it in the key of your record, it's redundant, and it won't go anywhere unless u delete the entry. 我建议删除对象的ID属性,因为你将它存储在记录的密钥中,它是多余的,除非你删除条目,否则它不会去任何地方。

data[ID] = row;

To delete the ID attribute (optional): 要删除ID属性(可选):

row.ID = null;
delete row.ID;

Then iterate through it using 然后使用迭代它

for(let key in data){}

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

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