6

Is it possible to destructure an object in Javascript by using a property name stored in a variable?

This is how we destructure at the moment.

const myObject = {a: 1, b: 2, c: 3};
let {a, b, c} = myObject;
console.log(a, b, c);

I would like to be able to store property names in variables:

const myObject = {banana: 1, apple: 2, strawberry: 3};
const chosenFruit = "banana";
const { [[chosenFruit]], ...theRest } = myObject;
console.log(banana); // Should be 1

For full disclosure, the reason I want to be able to do this is I want to remove the property "banana" from the object. So in my use case I want to be left with the theRest object which doesn't include the banana property, but there have been times I wanted to iterate through an array of values (e.g. days of the week) and programatically destructure objects quickly.

Is this possible? If so, how?

Thanks!

8
  • delete myObject.banana; <= done Commented Nov 18, 2020 at 14:53
  • @Taplar you mean delete myObject[choosenFruit] Commented Nov 18, 2020 at 14:55
  • You did say your goal was to remove banana from the object. Commented Nov 18, 2020 at 14:55
  • 1
    Are you after something like const { [chosenFruit]: banana, ...theRest } = myObject; ? you would have to explicitly type banana in the destructuring though Commented Nov 18, 2020 at 14:56
  • 1
    So you could clone it and then remove it Commented Nov 18, 2020 at 14:57

4 Answers 4

14

You could take a computed property names and rename the property (assigning to new variable names).

const myObject = { banana: 1, apple: 2, strawberry: 3 };
const chosenFruit = "banana";
const { [chosenFruit]: fruit, ...theRest } = myObject;

console.log(fruit); // 1
console.log(theRest);

Sign up to request clarification or add additional context in comments.

Comments

1

const myObject = {banana: 1, apple: 2, strawberry: 3};
const chosenFruit = "banana";
const newThing = { ...myObject };

delete newThing[chosenFruit];

console.log(myObject, newThing);

This version uses deconstruction to clone, and then removes the unwanted property.

9 Comments

A good solution! Achieves the desired outcome with deleting the property, but doesn't translate to "normal" destructuring, unlike Nina's answer. :)
"normal" deconstruction?
Where you just want to pull values out of objects, rather than deleting them.
Ah, I gotcha. True.
This doesnt have any destructuring
|
0

A variation with destructuring parameters:

const pick = (fruit, {[fruit]: out, ...fruits}) => [out, fruits];

const [fruit, fruits] = pick('banana', basket);

console.log(fruit);
console.log(fruits);
console.log(basket);
<script>const basket = { banana: 1, apple: 2, strawberry: 3 };</script>

Comments

0

This also works for non-dynamic choices and also if you want to have the variable name same as the fruit name

const myObject = { banana: 1, apple: 2, strawberry: 3 };
const { banana, ...theRest } = myObject;

console.log(banana); // 1
console.log(theRest);

1 Comment

The question was asking specifically for a way to do this with the property name in a variable. I.e., sometimes that var will have the value 'banana', and sometimes it will have the value 'apple'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.