![](/img/trans.png)
[英]return key value pair from object whose key matches certain condition with javascript and underscore
[英]Return object from array that matches key:value pair of an object nested in the object I need to return
期望狀態
我試圖從匹配特定值的數組中返回一個對象,但是由於該數組包含嵌套在另一個對象中的對象,因此我遇到了一些問題。 我想返回整個對象,但是我需要搜索的值是在嵌套對象內部。
例如,我試圖基於key:value對“ name”:“ Kills”的值返回整個對象。
{
"metadata": {
"key": "kills",
"name": "Kills",
"isReversed": false
},
"value": 1364,
"displayValue": "1,364"
}
數組格式
let stats = [
{
"metadata": {
"key": "kills",
"name": "Kills",
"isReversed": false
},
"value": 1364,
"displayValue": "1,364"
},
{
"metadata": {
"key": "score",
"name": "Score",
"isReversed": false
},
"value": 413743,
"displayValue": "413,743"
},
{
"metadata": {
"key": "matchesPlayed",
"name": "Matches Played",
"isReversed": false
},
"value": 2160,
"displayValue": "2,160"
}
]
當前代碼無效
我不打算使用這種結構的代碼,任何可行的解決方案對我來說都是很棒的,但我需要它成為可以重用的功能。
function getStatsFields(value, statsSegment){
console.log('getStatsFields ran', statsSegment.stats[0].metadata.name);
console.log('statsSegment.stats.length', statsSegment.stats.length);
var filteredStats = []
for(var i=0; i < statsSegment.stats.length; ++i){
const killsKey = Object.keys(statsSegment.stats[i].metadata.name)
console.log('killsKey', killsKey);
filteredStats = statsSegment.stats.filter(val => val[killsKey] === value)
console.log('filteredStats before if', filteredStats);
if(filteredStats.length){
console.log('filteredStats[i]', filteredStats[i]);
return filteredStats[i];
}
}
}
任何幫助將不勝感激!
嘗試
stats.find(x=> x.metadata.name=="Kills");
let stats= [ { "metadata": { "key": "kills", "name": "Kills", "isReversed": false }, "value": 1364, "displayValue": "1,364" }, { "metadata": { "key": "score", "name": "Score", "isReversed": false }, "value": 413743, "displayValue": "413,743" }, { "metadata": { "key": "matchesPlayed", "name": "Matches Played", "isReversed": false }, "value": 2160, "displayValue": "2,160" } ] let r= stats.find(x=> x.metadata.name=="Kills"); console.log(r);
如果您正在尋找一種根據元數據( key
, value
)對進行過濾的方法,那么下一個可以幫助您:
const stats = [ { "metadata": { "key": "kills", "name": "Kills", "isReversed": false }, "value": 1364, "displayValue": "1,364" }, { "metadata": { "key": "score", "name": "Score", "isReversed": false }, "value": 413743, "displayValue": "413,743" }, { "metadata": { "key": "matchesPlayed", "name": "Matches Played", "isReversed": false }, "value": 2160, "displayValue": "2,160" } ]; const filterByMetaKeyVal = (arr, metakey, v) => { return arr.filter(({metadata}) => metadata[metakey] === v); } console.log( "Filter by [name, Kills]:", filterByMetaKeyVal(stats, "name", "Kills") ); console.log( "Filter by [key, matchesPlayed]:", filterByMetaKeyVal(stats, "key", "matchesPlayed") ); console.log( "Filter by [isReversed, true]:", filterByMetaKeyVal(stats, "isReversed", true) );
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.