我正在尝试根据日期获取失败的详细信息。 为此,我正在应用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;
}
这将返回空数组。
有人可以找出我要去哪里的地方吗?
答案 0 :(得分:1)
在函数isPresent
中,value
是数组中的每个对象
您正在将整个对象与一个值进行比较
您需要做的是比较该对象的对象属性
function isPresent(value) {
return value.Date == "02/04/2019";
}
答案 1 :(得分:1)
您需要检查value.Date
而不是value
,因为value
或回调参数将保留object
中整个迭代的array
:>
function isPresent(value) {
return value.Date && value.Date == "02/04/2019";
}
要从过滤后的FailureDeatils
中仅获取object
,您可以映射
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);
答案 2 :(得分:1)
这是基于您的数组的工作代码。
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);
答案 3 :(得分:0)
var filtered = value.filter(function(a){
return a.Date === "02/04/2019";
});
答案 4 :(得分:0)
您应该这样做-
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 :(得分:0)
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 :(得分:0)
您可以这样做:
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);