如果resolveDesc字符串不包含“:”冒号,则从数组中删除该条目,而在以下代码中则不会发生,在以下代码中实现错误的是它返回了两个元素?
main.js
const messages = [{
"settlementCode": "85",
"settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85)||Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)"
},
{
"settlementCode": "99",
"settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85):Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)"
}
]
function validateEntries (messages) {
if(!messages){ return [] };
let filteredMsg = messages.filter(item => !item.settlementDesc.includes(":"))
return filteredMsg;
};
console.log(validateEntries(messages));
预期输出
由于结点描述包含冒号,因此不应省略,我们应该过滤掉响应。
[
{
"settlementCode": "99",
"settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85):Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)"
}
]
答案 0 :(得分:0)
使用过滤器
const messages = [{
"settlementCode": "85",
"settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85)||Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)"
},
{
"settlementCode": "99",
"settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85):Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)"
}
]
console.log(messages.filter(e => e.settlementDesc.includes(':')))
更正您的代码 将变量名称更改为包含对象的消息
const messages = [{
"settlementCode": "85",
"settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85)||Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)"
},
{
"settlementCode": "99",
"settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85):Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)"
}
]
function validateEntries(messages) {
if (!messages) {
return []
};
let filteredMsg = messages.filter(item => item.settlementDesc.includes(":"))
return filteredMsg;
};
console.log(validateEntries(messages));
答案 1 :(得分:0)
您的代码几乎在那里。只需将messages
而不是rejectMessage
传递给函数:
const messages= [{ "settlementCode": "85", "settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85)||Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)" }, { "settlementCode": "99", "settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85):Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)" } ]
function validateEntries (messages) {
if(!messages){ return [] };
let filteredMsg = messages.filter(item => item.settlementDesc.includes(":"))
return filteredMsg;
};
console.log(validateEntries(messages))
您还可以重构一些代码以使其更简洁:
const messages= [{ "settlementCode": "85", "settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85)||Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)" }, { "settlementCode": "99", "settlementDesc": "Sorry, the system is temporarily unavailable to process your request.(Error Code 85):Lo sentimos,Intente de nuevo más tarde. Gracias. (Código de error 85)" } ]
let validateEntries = msgs => !msgs
? []
: msgs.filter(item => item.settlementDesc.includes(":"))
console.log(validateEntries(messages))
答案 2 :(得分:-1)
删除!从过滤器功能。并确保将正确的变量传递给函数。它为我工作。
function validateEntries (messages) {
if(!messages){ return [] };
let filteredMsg = messages.filter(item => item.settlementDesc.includes(":"))
return filteredMsg
};
validateEntries(messages);