Created
January 29, 2020 20:36
-
-
Save fidel-karsto/e6fc134522fc01ab32a49f683fd4a20a to your computer and use it in GitHub Desktop.
Simple array destructuring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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