0
我沒有任何運氣來識別數組裏面和數組中定義的對象。Lodash _.filter如何找到數組中的兄弟姐妹
,我有以下數據
var values =
[
{
"letter": "S",
"clients": [
{
"_id": "58f61681e32f1927a41c19bf",
"firstName": "Shelly",
"lastName": "R",
}
]
},
{
"letter": "G",
"clients": [
{
"_id": "58f2ab99319e35299b1ee4a9",
"firstName": "Gary",
"lastName": "R",
}
]
}
]
及以下lodash電話。
var identifier = '58f2ab99319e35299b1ee4a9';
var v = _.filter(values, {clients: [{_id: identifier}]});
我讀的應該與嵌套數組一起工作。 我錯過了什麼嗎?
嗯,我在角組件路由場景中使用這個_.filter。
儘管values.clients對象包含更多信息。我用實際輸出替代了小提琴,它工作。返回一個實例。但是下面的代碼不會。
}).state('clients.client', {
url: '/{clientId}',
component: 'client',
resolve: {
client : function(rolodex, $stateParams, _){
//var values = rolodex;
//console.log(JSON.stringify(values));
var identifier = $stateParams.clientId;
var v = _.filter(rolodex, {clients: [{_id: identifier}]});
console.log(v[0].clients[0]);
return v[0].clients[0];
}
}
});
G.
我想我說話很快,這lodash腳本無法正常工作。 如果我的輔助數組包含多個條目,我在我的結果集中同時獲得了兩個條目。
[
{
"letter": "C",
"clients": [
{
"_id": "58f6b8a932edbb7dd718f05a",
"firstName": "Casie",
"lastName": "L"
},
{
"_id": "58f68b0d527cc336a8f86cc8",
"firstName": "Chad",
"lastName": "M"
}
]
},
{
"letter": "G",
"clients": [
{
"_id": "58f2ab99319e35299b1ee4a9",
"firstName": "Gary",
"lastName": "R"
}
]
},
{
"letter": "J",
"clients": [
{
"_id": "58f6b90032edbb7dd718f05c",
"firstName": "Jett",
"lastName": "F"
}
]
},
{
"letter": "S",
"clients": [
{
"_id": "58f61681e32f1927a41c19bf",
"firstName": "Shelly",
"lastName": "R"
},
{
"_id": "58f6b8d632edbb7dd718f05b",
"firstName": "Shirley",
"lastName": "A"
}
]
}
]
給我正確的數組包含正確的_id。
[
{
"letter": "S",
"clients": [
{
"_id": "58f61681e32f1927a41c19bf",
"firstName": "Shelly",
"lastName": "R"
},
{
"_id": "58f6b8d632edbb7dd718f05b",
"firstName": "Shirley",
"lastName": "A"
}
]
}
]
但我希望我的結果看起來像這樣。
{
"_id": "58f61681e32f1927a41c19bf",
"firstName": "Shelly",
"lastName": "R"
}
這可能是我的答案。
var b = _(data).thru(function(col1){
return _.union(col1, _.map(col1, 'clients'));
})
.flatten()
.find({_id : '58f61681e32f1927a41c19bf'});
這是Angular的不可知論。 –
這應該工作,[檢查這裏](https://jsfiddle.net/6qLze9ub/40/) –
你的正確它確實在那裏工作,但它似乎不太在我的代碼。 brb – muscleman71