我有嵌套的数据数组。我可以根据自己的条件获取第一组数据,但是当我在此结果集上应用array.filter时,它并没有给我预期的结果。
下面是数据数组:
get_in
首先,我已将以下array.filter应用到基于日期的FailureDetails:
[]
这给了我带有描述的失败详细信息,但是现在在原因ID上,我想获取描述,但是它不起作用并将结果返回为“ undefined”
谁能建议我要去哪里错了?
答案 0 :(得分:3)
您需要执行value.Date
而不是data.Date
,因为value
引用了data
数组中的每个对象:
var data = [{
"Date": "02/04/2019",
"Total": "1000",
"Success": "850",
"Failure": "150",
"FailureDeatils": [{
"name": "Reason1",
"Count": 2,
"Description": [{
"DescriptionID": "01",
"Status": "Failure Point1"
}, {
"DescriptionID": "01",
"Status": "Failure Point1"
}]
}, {
"name": "Reason2",
"Count": 6,
"Description": [{
"DescriptionID": "01",
"Status": "Failure Point1"
}, {
"DescriptionID": "01",
"Status": "Failure Point1"
}]
}]
}, {
"Date": "03/04/2019",
"Total": "800",
"Success": "750",
"Failure": "150",
"FailureDeatils": [{
"name": "Reason1",
"Count": 3,
"Description": [{
"DescriptionID": "01",
"Status": "Failure Point1"
}, {
"DescriptionID": "01",
"Status": "Failure Point1"
}]
}, {
"name": "Reason2",
"Count": 1,
"Description": [{
"DescriptionID": "01",
"Status": "Failure Point1"
}, {
"DescriptionID": "01",
"Status": "Failure Point1"
}]
}]
}];
var filtered = data
.filter(value => value.Date == "02/04/2019")
.map(e => e.FailureDeatils);
console.log(filtered);
答案 1 :(得分:2)
看起来您的意思是value.Date
而不是data.Date
。
.filter(value => value.Date == "02/04/2019")
答案 2 :(得分:0)
您在过滤器中输入了错误的变量:
应为:
const filtered = data.filter(value => value.Date == "02/04/2019").map(e => e.FailureDeatils);
而非:
const filtered = data.filter(value => data.Date == "02/04/2019").map(e => e.FailureDeatils);
以下是根据您的代码提供的有效的示例:
const object = {
"data": [
{
"Date": "02/04/2019",
"Total": "1000",
"Success": "850",
"Failure": "150",
"FailureDeatils": [
{
"name": "Reason1",
"Count": 2,
"Description": [
{
"DescriptionID": "01",
"Status": "Failure Point1"
},
{
"DescriptionID": "01",
"Status": "Failure Point1"
}
]
},
{
"name": "Reason2",
"Count": 6,
"Description": [
{
"DescriptionID": "01",
"Status": "Failure Point1"
},
{
"DescriptionID": "01",
"Status": "Failure Point1"
}
]
}
]
},
{
"Date": "03/04/2019",
"Total": "800",
"Success": "750",
"Failure": "150",
"FailureDeatils": [
{
"name": "Reason1",
"Count": 3,
"Description": [
{
"DescriptionID": "01",
"Status": "Failure Point1"
},
{
"DescriptionID": "01",
"Status": "Failure Point1"
}
]
},
{
"name": "Reason2",
"Count": 1,
"Description": [
{
"DescriptionID": "01",
"Status": "Failure Point1"
},
{
"DescriptionID": "01",
"Status": "Failure Point1"
}
]
}
]
}
]
}
const filtered = object.data.filter(value => value.Date == "02/04/2019").map(e => e.FailureDeatils);
console.log(filtered);