2017-03-07 92 views
0

我想篩選具有2個級別的子級的數組。我能夠過濾數組的第一層,但不是第二層。我正在嘗試返回collaborators下具有特定email的數組。這裏是結構基於第二級陣列鍵的Javascript數組篩選器

enter image description here

這裏是代碼。此代碼不匹配email所以返回數組中的所有項目

this.firebase.list('/todo'). 
map(data => data.filter((e) => { 
    return e.collaborators.filter((x) => { 
     return x.email == email; 
    }); 
})); 

樣品JSON:

{ 
    "todo" : { 
    "-Kee7m7WkNDhrbX_0Ycb" : { 
     "collaborators" : [ { 
     "email" : "[email protected]", 
     "name" : "test" 
     }, 
     { 
     "email" : "[email protected]", 
     "name" : "test1" 
     } ], 
     "created_at" : 1488913112569, 
     "created_by" : "[email protected]", 
     "preview" : "", 
     "title" : "test", 
     "updated_at" : 1488913112569 
    }, 
    "-Kee7m7WkNDhrbX_0Ycb" : { 
     "collaborators" : [ { 
     "email" : "[email protected]", 
     "name" : "test" 
     }, 
     { 
     "email" : "[email protected]", 
     "name" : "test2" 
     } ], 
     "created_at" : 1488913112569, 
     "created_by" : "[email protected]", 
     "preview" : "", 
     "title" : "test", 
     "updated_at" : 1488913112569 
    } 
    } 
} 

需要的輸出:

只有那些具有電子郵件的第一陣列[email protected]

+1

請提供可以剪切和粘貼的數據,而不是數據的屏幕截圖 –

+0

請不但要張貼輸入,還要張貼所需的輸出。 – Bergi

回答

1

我不確定你想要的輸出是什麼,但希望下面的例子可以幫助你。

希望這會有所幫助。

var obj = { 
 
    "todo" : { 
 
    "-Kee7m7WkNDhrbX_0Yca" : { 
 
     "collaborators" : [ { 
 
     "email" : "[email protected]", 
 
     "name" : "test" 
 
     }, 
 
     { 
 
     "email" : "[email protected]", 
 
     "name" : "test1" 
 
     } ], 
 
     "created_at" : 1488913112569, 
 
     "created_by" : "[email protected]", 
 
     "preview" : "", 
 
     "title" : "test", 
 
     "updated_at" : 1488913112569 
 
    }, 
 
    "-Kee7m7WkNDhrbX_0Ycb" : { 
 
     "collaborators" : [ { 
 
     "email" : "[email protected]", 
 
     "name" : "test" 
 
     }, 
 
     { 
 
     "email" : "[email protected]", 
 
     "name" : "test2" 
 
     } ], 
 
     "created_at" : 1488913112569, 
 
     "created_by" : "[email protected]", 
 
     "preview" : "", 
 
     "title" : "test", 
 
     "updated_at" : 1488913112569 
 
    } 
 
    } 
 
}; 
 

 
var email = "[email protected]"; 
 
var result = []; 
 

 
for(var key in obj["todo"]) { 
 
    obj["todo"][key].collaborators.filter((x) => { 
 
    if (x.email == email) 
 
     result.push(obj["todo"][key]); 
 
    }); 
 
} 
 

 
console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

'例外:無法讀取未定義的屬性'電子郵件' –

+0

好的請發佈您的對象。 –

+0

問題已更新 –

0

您希望filter回調返回一個布爾值,而不是一個數組。因此,在協作者上使用someevery而不是filter

相關問題