【问题标题】:Array.filter is returning empty arrayArray.filter 返回空数组
【发布时间】:2019-09-13 09:50:15
【问题描述】:

我正在尝试根据日期获取失败详细信息。 为此,我正在应用 array.filter 但它返回的是空数组。

下面是我的数组:

value:[{
  "Date": "02/04/2019",
  "Total": "1000",
  "Success": "850",
  "Failure": "150",
  "FailureDeatils": [{
      "Reason": "Reason1",
      "Count": 2
    },
    {
      "Reason": "Reason2",
      "Count": 6
    }
  ]
}, {
  "Date": "03/04/2019",
  "Total": "800",
  "Success": "750",
  "Failure": "150",
  "FailureDeatils": [{
    "Reason": "Reason1",
    "Count": 3
  }, {
    "Reason": "Reason2",
    "Count": 1
  }]
}]      

如果我将日期设为 02/04/2019,它应该返回以下内容:

{
  "FailureDeatils": [{
      "Reason": "Reason1",
      "Count": 2
    },
    {
      "Reason": "Reason2",
      "Count": 6
    }
  ]
}

我正在使用下面的 array.filter 方法:

var filtered = value.filter(isPresent);
function isPresent(value) {
  return value == 02/04/2019;
}

这是返回空数组。

谁能帮我弄清楚我哪里出错了?

【问题讨论】:

  • return value.Date == "02/04/2019"; 也许?
  • 什么是isPresent?请分享您使用 isPresent 申请的条件
  • is Present 只不过是下面写的函数
  • 好的,我已经根据您的代码添加了答案。在代码中实现之前,您也可以在浏览器控制台上对其进行测试。
  • 如果对您有用,请为该问题投票。谢谢!

标签: javascript arrays date object filtering


【解决方案1】:

在您的函数isPresent 中,value 是数组中的每个对象

你正在将整个对象与一个值进行比较

你需要做的是比较那个对象的对象属性

function isPresent(value) {
    return value.Date == "02/04/2019";
}

【讨论】:

  • 谢谢!!我现在可以获取全部数据,但我只需要其中的失败详细信息
【解决方案2】:
var filtered = value.filter(function(a){
                   return a.Date === "02/04/2019";
               });

【讨论】:

  • 02/04/2019 将在此处计算,请参阅console.log(02/04/2019) 的结果
【解决方案3】:

您需要检查value.Date 而不是value,因为value 或回调参数将保存来自array 的整个迭代object

function isPresent(value) {
  return value.Date && value.Date == "02/04/2019";
}

要从过滤后的object 中仅获取FailureDeatils,您可以映射 Array#map() method 过滤后的结果:

var filtered = data.filter(isPresent).map(o => { return {"FailureDeatils": o.FailureDeatils}});

注意:

  • 确保将"02/04/2019"包裹在""之间,这样它就可以被评估为string并正确比较,否则将被计算并视为Number并给出错误的过滤结果。
  • 避免对数组使用相同的变量名value 两次 用于回调参数,以提高代码的可读性。

演示:

var data = [{
  "Date": "02/04/2019",
  "Total": "1000",
  "Success": "850",
  "Failure": "150",
  "FailureDeatils": [{
      "Reason": "Reason1",
      "Count": 2
    },
    {
      "Reason": "Reason2",
      "Count": 6
    }
  ]
}, {
  "Date": "03/04/2019",
  "Total": "800",
  "Success": "750",
  "Failure": "150",
  "FailureDeatils": [{
    "Reason": "Reason1",
    "Count": 3
  }, {
    "Reason": "Reason2",
    "Count": 1
  }]
}]



var filtered = data.filter(isPresent).map(e => { return {"FailureDeatils": e.FailureDeatils}});

function isPresent(value) {
  return value.Date && value.Date == "02/04/2019";
}

console.log(filtered);

【讨论】:

    【解决方案4】:

    你应该这样做 -

    const value = [{
      "Date": "02/04/2019",
      "Total": "1000",
      "Success": "850",
      "Failure": "150",
      "FailureDeatils": [{
          "Reason": "Reason1",
          "Count": 2
        },
        {
          "Reason": "Reason2",
          "Count": 6
        }
      ]
    }, {
      "Date": "03/04/2019",
      "Total": "800",
      "Success": "750",
      "Failure": "150",
      "FailureDeatils": [{
        "Reason": "Reason1",
        "Count": 3
      }, {
        "Reason": "Reason2",
        "Count": 1
      }]
    }]
    
    const filtered = value.filter((x) => x.Date === "02/04/2019");
    

    【讨论】:

      【解决方案5】:

      const value = [{
            "Date": "02/04/2019",
            "Total": "1000",
            "Success": "850",
            "Failure": "150",
            "FailureDeatils": [{
                "Reason": "Reason1",
                "Count": 2
              },
              {
                "Reason": "Reason2",
                "Count": 6
              }
            ]
          }, {
            "Date": "03/04/2019",
            "Total": "800",
            "Success": "750",
            "Failure": "150",
            "FailureDeatils": [{
              "Reason": "Reason1",
              "Count": 3
            }, {
              "Reason": "Reason2",
              "Count": 1
            }]
          }]   
      
          var filtered = value.filter(isPresent).map(row => {
            return {
              FailureDeatils : row.FailureDeatils
            }
          });
          function isPresent(value) {
            return value.Date === "02/04/2019";
          }
      
          console.log(filtered)

      【讨论】:

        【解决方案6】:

        你可以这样做:

        const value = [{"Date": "02/04/2019","Total": "1000","Success": "850","Failure": "150","FailureDeatils":[{"Reason":"Reason1","Count":2}, {"Reason":"Reason2","Count":6}]},{"Date": "03/04/2019","Total": "800","Success": "750","Failure": "150","FailureDeatils": [{"Reason":"Reason1","Count":3},{"Reason":"Reason2","Count":1}]}];
        const isPresent = ({Date}) => Date === '02/04/2019';
        const filtered = value.filter(isPresent);
        
        console.log(filtered);

        【讨论】:

          【解决方案7】:

          这是基于您的数组的工作代码。

          var value = [{
            "Date": "02/04/2019",
            "Total": "1000",
            "Success": "850",
            "Failure": "150",
            "FailureDeatils": [{
                "Reason": "Reason1",
                "Count": 2
              },
              {
                "Reason": "Reason2",
                "Count": 6
              }
            ]
          }, {
            "Date": "03/04/2019",
            "Total": "800",
            "Success": "750",
            "Failure": "150",
            "FailureDeatils": [{
              "Reason": "Reason1",
              "Count": 3
            }, {
              "Reason": "Reason2",
              "Count": 1
            }]
          }];
          
          var filtered = value.filter (isPresent).map (obj => obj.FailureDeatils);;
          function isPresent (value) {
            return value.Date == "02/04/2019";
          }
          
          console.log (filtered);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-11-13
            • 1970-01-01
            • 2017-07-12
            • 1970-01-01
            • 1970-01-01
            • 2021-08-11
            • 2017-02-27
            相关资源
            最近更新 更多