Skip to content

Instantly share code, notes, and snippets.

@fidel-karsto
Created January 29, 2020 20:36
Show Gist options
  • Save fidel-karsto/e6fc134522fc01ab32a49f683fd4a20a to your computer and use it in GitHub Desktop.
Save fidel-karsto/e6fc134522fc01ab32a49f683fd4a20a to your computer and use it in GitHub Desktop.
Simple array destructuring
const booksList = [
'1',
'The Stranger\nThe Outsider',
'Albert Camus',
'1942',
'French',
'2',
'In Search of Lost Time\nRemembrance of Things Past',
'Marcel Proust',
'1913–27',
'French',
'3',
'The adventures of Huckleberry Finn',
'Mark Twain',
'1885',
'English'
];
function createBooksFromList(listOfBooks) {
const [id, title, author, year, language, ...rest] = listOfBooks;
const result = [];
result.push({
id,
title,
author,
year,
language
});
if (rest.length) {
return result.concat(createBooksFromList(rest));
}
return result;
}
console.log(createBooksFromList(booksList));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment