是否有可能迭代两个数组来匹配值?
我的火力DATABSE如下:是否有可能迭代两个数组来匹配值?
Users {
askdfasdf: John: {
Selection: [1,2,3]
},
fasfadffe: Mark: {
Selection: [1,2,4]
}
}
Players {
{
name: 'Messi',
agility: 90,
id: 1
},
{
name: 'Beckham',
agility: 54,
id: 2
},
{
name: 'Rooney',
agility: 10,
id: 3
},
{
name: 'Neymar',
agility: 84,
id: 4
}
}
数据库节点通过下面的代码带入范围:
var ref = firebase.database().ref("players");
var ref3 = firebase.database().ref("users").child(uid).child("total");
$scope.players = $firebaseArray(ref);
$scope.selection = $firebaseArray(ref3);
是否存在通过搜索两个数组的方式来itereate或环匹配值?具体来说,是否有一种方法可以通过“players”数组来循环搜索id
与“selection”数组中的数字相匹配的球员。
最终目标是让每个客户的选择在选择完成后反映在页面上。
我DATABSE安全规则如下:
{
"rules": {
"players":{
".read" : "auth != null",
".write" : "auth != null",
".indexOn": "id"
}
}
}
我试图遍历每个选择如下:
$scope.getSelectedPlayers = function(){
for (let i = 0; i<$scope.selection.length; i++){
return $scope.selection[i];
var ref=
firebase.database().ref("players").orderByChild("id").equalTo($scope.selection[i]);
}
但这并没有工作
你可以使用一个对象与id
作为单人的关键。
通过迭代users
,您可以通过将id
添加到玩家信息中来添加所需的信息。
var players = [{ name: 'Messi', agility: 90, id: 1 }, { name: 'Beckham', agility: 54, id: 2 }, { name: 'Rooney', agility: 10, id: 3 }, { name: 'Neymar', agility: 84, id: 4 }],
object = Object.create(null);
players.forEach(p => object[p.id] = p);
console.log(object);
对不起,我不知道firebase。 –
您可以查询使用equalTo
每个选定的ID,记住你有指标Player.id在火力安全规则。
然后简单:
firebase.database().ref("players").orderByChild("id").equalTo('selected_id_here');
你可以找到更多的火力文档:here
谢谢,但我不太清楚如何在安全规则中为Player.id编制索引。文章相当混乱。对不起,如果这是一个愚蠢的问题,但我可能会看到如何做到这一点? –
在此处查看:[indexing-data](https://firebase.google.com/docs/database/security/indexing-data) – Khaled
谢谢。是的,我读过,很困惑。我编辑了我的问题,以反映我现在的规则。这是正确的语法吗? –
@NinaScholz对不起你能详细谈谈那一点?我不太确定这是什么意思? –
对不起,这只是想。 –