Selective object destructuring with assignment in JavaScript?

Ismael

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!

T.J. Crowder

In terms of destructuring, it doesn't get a lot better:

let displayInfo = {};
({title: displayInfo.title, author: displayInfo.author} = book);

Live Example:

const book = {
  "title" : "The Pillars of the Earth",
  "author" : "Ken Follet",
  "year" : 1989,
  "genres" : ["fiction", "historical"]
};

let displayInfo = {};
({title: displayInfo.title, author: displayInfo.author} = book);

console.log(displayInfo);

But you can give yourself a helper function:

function pick(source, ...props) {
    const obj = {};
    for (const prop of props) {
        obj[prop] = source[prop];
    }
    return obj;
}

then it's:

let displayInfo = pick(book, "title", "author");

Live Example:

const book = {
  "title" : "The Pillars of the Earth",
  "author" : "Ken Follet",
  "year" : 1989,
  "genres" : ["fiction", "historical"]
};

function pick(source, ...props) {
    const obj = {};
    for (const prop of props) {
        obj[prop] = source[prop];
    }
    return obj;
}

let displayInfo = pick(book, "title", "author");

console.log(displayInfo);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript object destructuring with assignment in Babel

Use of Colon in object assignment destructuring Javascript

Object property assignment with destructuring?

Destructuring assignment in object creation

JavaScript Destructuring Assignment

Destructuring Assignment to Pass an Object as a Function's Parameters in Javascript behaviour

How does dependent object destructuring assignment work in JavaScript?

Selective assignment of values to parameters in JavaScript

Nested in object array destructuring with assignment

Is the order of destructuring assignment in JavaScript guaranteed?

What's the difference between object destructuring and normal object assignment in Javascript ES6?

Javascript Replacement With Object Destructuring

JavaScript object destructuring and aliasing

javascript: object destructuring

Problems with JavaScript object destructuring

Partial object copying using Destructuring Assignment

destructuring assignment of array stored in object property

Destructuring assignment to costruct a new object - Is it possible?

Destructuring assignment - object properties to variables in C#

Destructuring assignment in function call while preserving the object

Object destructuring assignment with default value of itself

How to use a destructuring assignment inside an object

Array destructuring into object assignment in a single instruction

Object Destructuring Assignment Behaving Weirdly in Constructor

JavaScript Array destructuring assignment and null value

JavaScript destructuring assignment after using regex

Destructuring assignment for dict with default values in JavaScript

Javascript destructuring to populate existing object

destructuring nested object in javascript (react)