如何根据索引对象内的值获取特定的数组索引?柔性

时间:2022-01-26 07:52:24

So, for sending to individual streams we have to reference the connected netStream we want to send to in some way like this:

因此,为了发送到各个流,我们必须以某种方式引用我们想要发送到的连接的netStream:

sendStream.peerStreams[0].send("MyFunction",param1,param2);

and I have to determine which peer I'm sending to by their ID such as "peerID1234"

我必须确定我通过他们的ID发送到哪个对等方,例如“peerID1234”

I know that you can check the peerID of the stream by doing:

我知道您可以通过执行以下操作来检查流的peerID:

sendStream.peerStreams[0]["farID"]

how can I make my send stream function know to use the array index where the peerID is?

如何让我的发送流功能知道使用peerID所在的数组索引?

so basically it could be like:

所以基本上它可能是这样的:

sendStream.peerStreams[where peerStreams[]["farID"] == peerID].send("MyFunction",param1,param2);

1 个解决方案

#1


1  

Sounds like you'll have to loop through the peerStreams array to find the object that has the right farID property value. Basically you are searching through the array for an item with a specific property value. There is no built-in functionality for this. But you can do it with a simple loop. Something like this:

听起来你必须遍历peerStreams数组才能找到具有正确farID属性值的对象。基本上,您正在数组中搜索具有特定属性值的项目。没有内置功能。但是你可以通过一个简单的循环来完成它。像这样的东西:

var correctStream:Object = null;
for each (var stream:Object in sendStream.peerStreams) {
   if (stream["farId"] == peerId) {
     correctStream = stream;
     break;
   }
}
correctStream.send("MyFunction",param1,param2);

Note that I don't know what the data type is for the peerStreams object so I just typed it as Object in my example.

请注意,我不知道peerStreams对象的数据类型是什么,因此我只是在我的示例中将其键入为Object。

There's some other approaches mentioned here but they are just different styles of doing the same thing.

这里提到了一些其他的方法,但它们只是做同样事情的不同风格。

#1


1  

Sounds like you'll have to loop through the peerStreams array to find the object that has the right farID property value. Basically you are searching through the array for an item with a specific property value. There is no built-in functionality for this. But you can do it with a simple loop. Something like this:

听起来你必须遍历peerStreams数组才能找到具有正确farID属性值的对象。基本上,您正在数组中搜索具有特定属性值的项目。没有内置功能。但是你可以通过一个简单的循环来完成它。像这样的东西:

var correctStream:Object = null;
for each (var stream:Object in sendStream.peerStreams) {
   if (stream["farId"] == peerId) {
     correctStream = stream;
     break;
   }
}
correctStream.send("MyFunction",param1,param2);

Note that I don't know what the data type is for the peerStreams object so I just typed it as Object in my example.

请注意,我不知道peerStreams对象的数据类型是什么,因此我只是在我的示例中将其键入为Object。

There's some other approaches mentioned here but they are just different styles of doing the same thing.

这里提到了一些其他的方法,但它们只是做同样事情的不同风格。