0

const obj = {
  '0': '100',
  '1': '0',
  '2': '0'
};

console.log("defaultObj", obj);

const test1 = () => {
  return {
    0: amount,
    1: balance,
    2: products
  } = obj;
};

const test2 = () => {
  const {
    0: amount,
    1: balance,
    2: products
  } = obj;
  return {
    amount,
    balance,
    products
  }
};

console.log("test1", test1());
console.log("test2", test2());

The output is

defaultObj { '0': '100', '1': '0', '2': '0' }
test1 { '0': '100', '1': '0', '2': '0' }
test2 { amount: '100', balance: '0', products: '0' }

So what is the difference between test1 and test2?

How to return a destructured object without declaring them as variables like in test2.

The desired output is to convert test1 output to test2 by returning the destructured object right away

Without declaring them as variables is there a possible solution?

Not A Bot
  • 2,220
  • 2
  • 10
  • 24
Mustafa Agha
  • 78
  • 1
  • 5
  • 3
    `return { amount: obj[0], balance: obj[1], products: obj[2] }`? – user3840170 Jan 27 '21 at 11:37
  • 1
    What is wrong with the `destructure` function you have created? Seems to do what you are asking – adiga Jan 27 '21 at 11:42
  • This is not the solution I'm looking for, is there another way to work around this without accessing obj keys also you didn't destructure the obj. you just accessed it's keys values and assigned it to another object keys – Mustafa Agha Jan 27 '21 at 11:44
  • @adiga This is the one i'm using now but I think there is a better way – Mustafa Agha Jan 27 '21 at 11:45
  • 1
    Have you seen: [ES6 destructuring within a return statement](https://stackoverflow.com/q/41794086)? – Nick Parsons Jan 27 '21 at 11:45
  • 1
    Does this answer your question? [ES6 destructuring within a return statement](https://stackoverflow.com/questions/41794086/es6-destructuring-within-a-return-statement) – evolutionxbox Jan 27 '21 at 11:46
  • @NickParsons yes I took the destructure() function idea from this post. so I added this question to check if there is a better way – Mustafa Agha Jan 27 '21 at 11:46
  • 2
    Destructuring is just assigning properties to variables. Creating another object from the variables is another step. If you do too many things in one line, it will be confusing to people who read the code while there is no difference in performance in writing less code. You could convert it to an arrow function with implicit return: `const destructure = ({ 0: amount, 1: balance, 2: products }) => ({ amount, balance, products })` But, it's not a big improvement – adiga Jan 27 '21 at 11:47
  • @MustafaAgha Can you clarify the requirements? You want dynamic access to properties of the object but you want to change the key names in some fixed way? You should be careful of that, as you cannot always guarantee the order keys would be accessed when accessing them dynamically. This is fine if the key names stay the same, but can be problematic when you want to change them. – msg45f Jan 27 '21 at 11:47
  • @adiga thank you. the destructure function do the work, but it would be nicer if the code looks better. – Mustafa Agha Jan 27 '21 at 11:49
  • @msg45f yes I understand but this is the result of calling a getter in solidity contract. and it returns the object with keys numbers (0, 1, 2) and it's corresponding values – Mustafa Agha Jan 27 '21 at 11:50

1 Answers1

2

what is the difference between test1 and test2?

An assignment expression always returns the right hand side value. return (… = obj2) will always return the obj2 value no matter what is.

how to return a destructured object without declaring them as variables?

You seem to be confused about what destructuring does. There is no such thing as a "destructured object", when you de-structure the object it's not an object any longer. Its invidivual parts are getting assigned to plain variables.

In your test2 function, you then create a new object with the values from these variables, using an object literal with shorthand syntax.

There is no way to combine a destructuring target and an object literal into one expression.

If you don't want to introduce individual variables with destructuring (either in the parameter declaration or a separate variable declaration), just access the argument object properties directly as usual:

function test3(obj) {
  return { amount: obj[0], balance: obj[1], products: obj[2] };
}

(Taken from @user3840170's comment)

Bergi
  • 513,640
  • 108
  • 821
  • 1,164