【问题标题】:Return object from array that matches key:value pair of an object nested in the object I need to return从匹配键的数组返回对象:嵌套在我需要返回的对象中的对象的值对
【发布时间】:2019-03-01 05:36:10
【问题描述】:

理想状态

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

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

}

任何帮助将不胜感激!

【问题讨论】:

标签: javascript arrays node.js express


【解决方案1】:

试试

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

【讨论】:

    【解决方案2】:

    如果您正在寻找一种按元数据(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;}

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多