What if neither test succeed? You don't have a pure else statement in your example, so are you sure that undefined is allowed for item?
If so, as @marco-disco suggested, you can define a variable above your first if statement:
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,
};
}
If you really want item to be a const, you can use a ternary operator:
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
or use an IIFE (immediately invoked function expression):
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
}
})()
letabove theifstatement, and mutate it accordingly