let traveler = [{
timestamp: 'qualia',
firstname: 'unborn',
lastname: 'child',
location: 'null'
},
{
timestamp: 1000,
firstname: 'Olivia',
lastname: 'Kirshner',
location: 'Titan'
},
{
timestamp: 1001,
firstname: 'James',
lastname: 'Cole',
location: 'Emerson Hotel'
}]
// function noerror(splinter = traveler[0]) {
// return [{firstname, lastname}] = splinter
// }
function restructure(time) {
// if no array is returned on the lookup, and the value flags as undefined in restructure()
let missingLink = traveler[0]
let tempArray = traveler
.filter((item) => item.timestamp === time)
try {
if (tempArray.firstname !== 'undefined' || tempArray.firstname !== 'null') { // check does not work
return [{ firstname, lastname }] = tempArray
}
// else {
// return [{firstname, lastname}] = missingLink
// }
} catch (e) {
// e instanceof TypeError // boolean error type check
switch (e.name) {
case 'TypeError':
console.error(`Could not complete your request: ${e.message}`);
// return [{firstname, lastname}] = missingLink
default:
break;
}
}
}
restructure(1001)
console.log(`firstname: ${firstname}, lastname: ${lastname}`)
restructure(100)
console.log(`firstname: ${firstname}, lastname: ${lastname}`)
restructure(1000)
lastname = 'Redforester' // temporary assignment to the variable, by value, not to the Object
console.log(`firstname: ${firstname}, lastname: ${lastname}`)
restructure(1000)
42号线让我头疼。
当传递给过滤器的搜索键产生空白数组时,我试图从捕获的异常循环中返回默认值。
该错误由console.error()处理,因此运行代码可查看错误是什么。
您知道一种允许从捕获的异常循环返回默认值的方法吗?
该行已被注释掉,但是尝试它会导致新的错误:
return [{firstname, lastname}] = missingLink
答案 0 :(得分:1)
使用filter
返回一个数组。因此,tempArray
是一个数组,没有firstname
和lastname
属性。
为此,我建议您将filter()
切换为find()
,并在解构过程中删除[]
:
let traveler = [{
timestamp: 'qualia',
firstname: 'unborn',
lastname: 'child',
location: 'null'
},
{
timestamp: 1000,
firstname: 'Olivia',
lastname: 'Kirshner',
location: 'Titan'
},
{
timestamp: 1001,
firstname: 'James',
lastname: 'Cole',
location: 'Emerson Hotel'
}]
// function noerror(splinter = traveler[0]) {
// return [{firstname, lastname}] = splinter
// }
function restructure(time) {
// if no array is returned on the lookup, and the value flags as undefined in restructure()
let missingLink = traveler[0]
let tempArray = traveler
.find((item) => item.timestamp === time)
try {
if (tempArray.firstname !== 'undefined' || tempArray.firstname !== 'null') { // check does not work
return { firstname, lastname } = tempArray
}
// else {
// return [{firstname, lastname}] = missingLink
// }
} catch (e) {
// e instanceof TypeError // boolean error type check
switch (e.name) {
case 'TypeError':
console.error(`Could not complete your request: ${e.message}`);
return {firstname, lastname} = missingLink
default:
break;
}
}
}
restructure(1001)
console.log(`firstname: ${firstname}, lastname: ${lastname}`)
restructure(100)
console.log(`firstname: ${firstname}, lastname: ${lastname}`)
restructure(1000)
lastname = 'Redforester' // temporary assignment to the variable, by value, not to the Object
console.log(`firstname: ${firstname}, lastname: ${lastname}`)
restructure(1000)
答案 1 :(得分:0)
我相信您要搜索的关键字是
throw
包含捕获到catch(error){}
中的任何error
的{{1}}短语。
例如:
try{}
如果您没有从 try {
throw "me"
}
catch(error){
// value of error is "me"
console.log(error);
}
投掷任何东西,则传播将从此处停止。
因此,抛出“我们” 会将错误传播到父函数。因此:
catch