javascript - Array.filter 返回空数组

标签 javascript arrays date object filtering

我正在尝试根据日期获取失败详细信息。 为此,我正在应用 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;
}

这将返回空数组。

谁能告诉我我哪里出错了?

最佳答案

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

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

关于javascript - Array.filter 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55824276/

相关文章:

javascript - 从现有字符串创建字符串,其中每个字符都由 * 表示,只有空格由空格表示

php - 使用 PHP 的 DateTime 警告

javascript - Mean Stack - 使用 "Globals"模块会在运行 Web 应用程序时导致问题吗?

来自 google sheet 的 Javascript 分页 JSON 数据

javascript - 为什么 javascript 使用++ 对字符串和文字产生不同的错误?

javascript - jScript - 如何 "mirror"矩阵(多维数组)

使用atoi将char数组转换为整数导致段错误

java - 在 Java 中创建数组(C++ 风格)

excel - VBA - 更改日期语言

php - 将用户输入转换为我的日期格式