【问题标题】:How can I filter an object based upon value in ReactJS? [closed]如何根据 ReactJS 中的值过滤对象? [关闭]
【发布时间】:2020-05-28 13:57:12
【问题描述】:

我有以下物品。谁的值为YES,我需要对其进行过滤并将其存储在变量中。

"CalcResult": {
        "result": [
            {
                "IS_RPOP": "YES",
                "IS_ENE": "NO",
                "IS_SLO": "YES",
                "STATUS": "INCOMPLETE"
            }
        ],
        "Info": {
            "STATUS": [
                "INCOMPLETE"
            ]
        }
    }

【问题讨论】:

  • 你尝试了什么?你被困在哪里了?请提供您的尝试,我们将帮助您找出问题所在。
  • 你的预期输出是什么??

标签: javascript arrays json reactjs object


【解决方案1】:

let CalcResult = {
  "result": [{
    "IS_RPOP": "YES",
    "IS_ENE": "NO",
    "IS_SLO": "YES",
    "STATUS": "INCOMPLETE"
  }],
  "Info": {
    "STATUS": [
      "INCOMPLETE"
    ]
  }
};
let arr = [];
Object.entries(CalcResult.result[0]).map((property, value) => {
  if (property[1].toUpperCase() === "YES")
    arr.push({
      [property[0]]: property[1]
    })
});
console.log(CalcResult.result[0], 'json');
console.log(arr, 'arr');

【讨论】:

    【解决方案2】:

    看来你想要的是等于YES的键值,如果是的话你可以试试这个方法:

    let data ={ "result": [ { "IS_RPOP": "YES", "IS_ENE": "NO", "IS_SLO": "YES", "STATUS": "INCOMPLETE" } ], "Info": { "STATUS": [ "INCOMPLETE" ] } };
    var result = Object.fromEntries(Object.entries(data.result[0]).filter(([k,v])=>v=='YES'));
    
    console.log(result)

    【讨论】:

      猜你喜欢
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 2021-12-05
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      相关资源
      最近更新 更多