0

Is there a neater way to assign the values to objects rather than what I have:

My code:

let val1 = result.data.data.table.val1;
let val2 = result.data.data.table.test.val2;
let val3 = result.data.data.table.val3;
let val4 = result.data.data.table.val4;
let val5 = result.data.data.table.val5;

Is there a neater way to assign this, a one-liner?

6
  • 1
    If you find yourself making property/variable names like "val1", "val2", etc, you probably should consider using an array.
    – Pointy
    Commented May 16, 2023 at 20:15
  • Does the second one really have .test, unlike the others?
    – Barmar
    Commented May 16, 2023 at 20:15
  • @Barmar, test has another object, val2
    – nb_nb_nb
    Commented May 16, 2023 at 20:16
  • 1
    @Pointy I'm guessing that those are just example names, they're not really numbered.
    – Barmar
    Commented May 16, 2023 at 20:20
  • @Barmar well I've learned not to make too many assumptions around here
    – Pointy
    Commented May 16, 2023 at 20:22

1 Answer 1

3

I think what you're looking for is destructuring.

let {val1, test: {val2}, val3, val4, val5} = result.data.data.table;

Not the answer you're looking for? Browse other questions tagged or ask your own question.