I've stumbled several times already upon the following problem:
Let's assume we have the following object:
const book = {
"title" : "The Pillars of the Earth",
"author" : "Ken Follet",
"year" : 1989,
"genres" : ["fiction", "historical"]
}
I would like to extract on-the-fly only part of the information from the object. Obviously, I cannot use the Object.assign
because it's going to copy all the properties. The fastest, but not very elegant way of doing it I've found so far is destructuring and then the concise syntax for creating an object, for example:
let {title, author} = book;
let displayInfo = {title, author};
It works, though it's not very elegant, mostly because as a side-effect I've just created the title
and author
variables, which I probably don't really need.
Is there a smarter, more concise syntax for this operation? Like a secret one-liner?
Thank you in advance!