React Native - if / Else 带有一个变量



我必须创建两个不同的变量(具有相同的名称(作为axios(req数据(的选项。

var indentifiers = entry.volumeInfo.industryIdentifiers;
console.log(indentifiers.length);

if (indentifiers.length === 1) {
const item = {
isbn_13: 'Not Provided',
isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
kind: entry.kind,
title: entry.volumeInfo.title,
authors: entry.volumeInfo.authors,
publishedDate: entry.volumeInfo.publishedDate,
language: entry.volumeInfo.language,
image1: entry.volumeInfo.imageLinks.thumbnail,
};
} else if (indentifiers.length === 2) {
const item = {
isbn_13: entry.volumeInfo.industryIdentifiers[1].identifier,
isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
kind: entry.kind,
title: entry.volumeInfo.title,
authors: entry.volumeInfo.authors,
publishedDate: entry.volumeInfo.publishedDate,
language: entry.volumeInfo.language,
image1: entry.volumeInfo.imageLinks.thumbnail,
};
}

公理:

const res = await axios.post(
'http://74c26025c4e2.ngrok.io/api/database/addbook',
item,
config,
);

显然,当我将变量放入 if/else 时,其余变量无法访问。我能做什么? 谢谢

如果两个测试都不成功怎么办?您的示例中没有纯else语句,因此您确定undefined允许item吗?

如果是这样,正如 @marco-disco 所建议的那样,您可以在第一个if语句上方定义一个变量:

let item
if (indentifiers.length === 1) {
item = {
isbn_13: 'Not Provided',
isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
kind: entry.kind,
title: entry.volumeInfo.title,
authors: entry.volumeInfo.authors,
publishedDate: entry.volumeInfo.publishedDate,
language: entry.volumeInfo.language,
image1: entry.volumeInfo.imageLinks.thumbnail,
};
} else if (indentifiers.length === 2) {
item = {
isbn_13: entry.volumeInfo.industryIdentifiers[1].identifier,
isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
kind: entry.kind,
title: entry.volumeInfo.title,
authors: entry.volumeInfo.authors,
publishedDate: entry.volumeInfo.publishedDate,
language: entry.volumeInfo.language,
image1: entry.volumeInfo.imageLinks.thumbnail,
};
}

如果你真的想item是一个常量,你可以使用三元运算符:

const item = (indentifiers.length === 1) ? {
const item = {
isbn_13: 'Not Provided',
isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
kind: entry.kind,
title: entry.volumeInfo.title,
authors: entry.volumeInfo.authors,
publishedDate: entry.volumeInfo.publishedDate,
language: entry.volumeInfo.language,
image1: entry.volumeInfo.imageLinks.thumbnail,
};
} : (indentifiers.length === 2) ? {
const item = {
isbn_13: entry.volumeInfo.industryIdentifiers[1].identifier,
isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
kind: entry.kind,
title: entry.volumeInfo.title,
authors: entry.volumeInfo.authors,
publishedDate: entry.volumeInfo.publishedDate,
language: entry.volumeInfo.language,
image1: entry.volumeInfo.imageLinks.thumbnail,
};
} : undefined

或使用 IIFE(立即调用的函数表达式(:

const item = (() => {
if (indentifiers.length === 1) {
return {
isbn_13: 'Not Provided',
isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
kind: entry.kind,
title: entry.volumeInfo.title,
authors: entry.volumeInfo.authors,
publishedDate: entry.volumeInfo.publishedDate,
language: entry.volumeInfo.language,
image1: entry.volumeInfo.imageLinks.thumbnail,
};
} else if (indentifiers.length === 2) {
return {
isbn_13: entry.volumeInfo.industryIdentifiers[1].identifier,
isbn_10: entry.volumeInfo.industryIdentifiers[0].identifier,
kind: entry.kind,
title: entry.volumeInfo.title,
authors: entry.volumeInfo.authors,
publishedDate: entry.volumeInfo.publishedDate,
language: entry.volumeInfo.language,
image1: entry.volumeInfo.imageLinks.thumbnail,
};
} else {
return undefined
}
})()

相关内容

最新更新