gpt4 book ai didi

javascript - 如何使用reduce从数组中删除元素?

转载 作者:行者123 更新时间:2023-12-02 21:45:09 25 4
gpt4 key购买 nike

尝试根据以下代码的某些逻辑过滤掉对象将返回空数组,尝试实现以下逻辑的任何帮助将不胜感激

1-如果第一个对象(firstFillIndicator = Y,acceptDigitalFirstFill = Y)和第二个对象(firstFillIndicator = Y,acceptDigitalFirstFill = N)。这个对象应该被删除它的假值。

2-如果第一个对象(firstFillIndicator = Y,acceptDigitalFirstFill = Y)和第二个对象(firstFillIndicator = N,acceptDigitalFirstFill = Y)。这个对象应该被删除它的假值。

3-如果两个对象都有firstFillIndicator 和acceptDigitalFirstFill 都有一些值为真值。

main.ts

function validateSingleOrder(data){
let isValid = false;
const filteredDrugs = data.filter((item) => {
isValid = isValidOrder(item);
if(isValid){
return true;
} else {
return false;
}
})

return filteredDrugs;
}

function isValidOrder(item){
item.rxInfos.reduce((a, c) => {
return (a.acceptDigitalFirstFill === "Y" && c.firstFillIndicator === "Y") || (a.firstFillIndicator === "N" && c.acceptDigitalFirstFill === "N")
});
}

console.log(validateSingleOrder(rxDetails));

数据

 const rxDetails = [
{
indexID: 1,
rxInfos: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "Y",
rxNumber: "1512"
},
{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "N",
rxNumber: "16021"

}
]
},
{
indexID: 2,
rxInfos: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "Y",
rxNumber: "1512"
},
{
firstFillIndicator: "N",
acceptDigitalFirstFill: "Y",
rxNumber: "16021"

}
]
},
{
indexID: 3,
rxInfos: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "Y",
rxNumber: "1512"
},
{
firstFillIndicator: "N",
acceptDigitalFirstFill: "N",
rxNumber: "16021"

}
]
}
]

预期结果

[{
indexID: 3
rxInfoss: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "Y",
rxNumber: "1512"
},
{
firstFillIndicator: "N",
acceptDigitalFirstFill: "N",
rxNumber: "16021"

}
]
},

];

最佳答案

问题是 isValidOrder 函数不会返回任何内容,无论如何,使用 .every(..) 比使用 更合适>.reduce(..) 在本例中,这是一个示例:

const rxDetails = [{
indexID: 1,
rxInfos: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "Y",
rxNumber: "1512"
},
{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "N",
rxNumber: "16021"

}
]
},
{
indexID: 2,
rxInfos: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "Y",
rxNumber: "1512"
},
{
firstFillIndicator: "N",
acceptDigitalFirstFill: "Y",
rxNumber: "16021"

}
]
},
{
indexID: 3,
rxInfos: [{
firstFillIndicator: "Y",
acceptDigitalFirstFill: "Y",
rxNumber: "1512"
},
{
firstFillIndicator: "N",
acceptDigitalFirstFill: "N",
rxNumber: "16021"

}
]
}
];


const result = rxDetails.filter(o => {
return o.rxInfos.every(v => {
return (v.firstFillIndicator === 'Y' && v.acceptDigitalFirstFill === 'Y') || (v.firstFillIndicator === 'N' && v.acceptDigitalFirstFill === 'N');
});
});

console.log(result);

关于javascript - 如何使用reduce从数组中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60271056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com