skip to Main Content

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?

2

Answers


  1. I think what you’re looking for is destructuring.

    let {val1, test: {val2}, val3, val4, val5} = result.data.data.table;
    
    Login or Signup to reply.
  2. NOTE: Please use the destructing code above to solve your problem, but the code below is an alternative if the the value that you are assigning changes.

    for(i = 1; i < 6; i++){let val = 'val' + i.toString();val = `something${1}`;}
    

    PS: the for loop is just an example you can pretty much use anything just be aware of what you are assigning and what the value should be, this isn’t the best solution but it is a solution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search