gpt4 book ai didi

javascript - 如何从捕获的异常循环中返回值?

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

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

最佳答案

使用filter返回一个数组。因此,tempArray 作为一个数组,没有 firstnamelastname 属性。

为此,我建议您将 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)

关于javascript - 如何从捕获的异常循环中返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283103/

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