这是我试图在这里If the "settlementDesc" in messages, has a string without ":" or " ", omit that entry of messages. If all entries are omitted in the messages, then remove the messages entry from the drug price response.
达到的结果,因此在下面的代码中,它执行相反的操作,知道实现了什么错误吗?
main.js
const messages = [
{
"settlementCode": "99",
"settlementDesc": "Not Covered: Call beneficios."
},
{
"settlementCode": "85",
"settlementDesc": ""
},
{
"settlementCode": "65",
"settlementDesc": ":"
}
];
function validateEntries (messages) {
if(!messages){ return [] };
const filteredMsg = messages.filter(mesg => {
if (checkString(mesg)) return mesg
} );
return filteredMsg;
};
const checkString = (mesg) => {
if (mesg.settlementDesc.indexOf(":") || mesg.settlementDesc.indexOf("")) {
return false;
} else {
return true;
}
};
validateEntries(messages);
预期输出
[
{
"settlementCode": "99",
"settlementDesc": "Not Covered: Call beneficios."
}
]
答案 0 :(得分:0)
不确定要什么,但是:
将字符串制成数组或对其进行过滤的一种方法是:
var myString = 'a b c';
console.log(myString.split(' '));
这将记录['a', 'b', 'c']
的数组。
希望有帮助!
答案 1 :(得分:0)
尝试使用include而不是indexOf,因为indexOf总是返回数字。
const messages = [
{
"settlementCode": "99",
"settlementDesc": "Not Covered: Call beneficios."
},
{
"settlementCode": "85",
"settlementDesc": ""
},
{
"settlementCode": "65",
"settlementDesc": ":"
}
];
function validateEntries (messages) {
if(!messages){ return [] };
const filteredMsg = messages.filter(mesg => {
if (checkString(mesg)) return mesg;
} );
return filteredMsg;
};
const checkString = (mesg) => {
if (mesg.settlementDesc.includes(":")) {
return true;
} else {
return false;
}
};
var output = validateEntries(messages);
console.log(output);
答案 2 :(得分:0)
您可以过滤邮件列表并仅返回需要的邮件
const messages = [
{
"settlementCode": "99",
"settlementDesc": "Not Covered: Call beneficios."
},
{
"settlementCode": "85",
"settlementDesc": ""
},
{
"settlementCode": "65",
"settlementDesc": ":"
}
];
let filtered = messages.filter(item => item.settlementDesc.includes(":"))