从与嵌套在我需要返回的对象中的对象的key:value对匹配的数组中返回对象

时间:2019-02-28 18:06:02

标签: javascript arrays node.js express

期望状态

我试图从匹配特定值的数组中返回一个对象,但是由于该数组包含嵌套在另一个对象中的对象,因此我遇到了一些问题。我想返回整个对象,但是我需要搜索的值是在嵌套对象内部。

例如,我试图根据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];
    }
  }

}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

尝试

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);

答案 1 :(得分:0)

如果您正在寻找一种根据元数据(keyvalue)对进行过滤的方法,那么下一个可能会帮助您:

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;}