6
useVim
2y

array destructuring people. please!

Comments
  • 1
    What about it?
  • 2
    Yes, I love it.

    const [ yes , ... other ] = array;
  • 0
    @ElectroArchiver me too, but with tuples in Swift :)

    let (firstValue, secondValue) = tuple
  • 0
    @Lensflare Mhm I see, can you also use something like the rest operator?
  • 1
    @ElectroArchiver I’m not sure how this would be useful in the context of destructoring, but since variadic parameters are ultimately arrays in Swift, you could assign them to a variable as well.
  • 1
    I like array constructuring in javascript:

    let arr1 = [a, b, c]

    let arr2 = [1,...arr1,3]
  • 1
    @Demolishun

    let arr1 = [a, b, c]
    let arr2 = [1] + arr1 + [3]

    No need for fancy operators 😄
  • 0
    nucular array destruction is where it's at!
  • 0
    @here

    mostly because I don't want to see this.
  • 1
    @useVim Even assigning a variable beforehand would be better than this
  • 0
    @useVim Where is that from?

    ( FSS devrant doesn't keep line spacing / indentation, fuck )

    Also I can't not refactor that:

    const { referralCode , email , role , name , _id } =

    request.auth.credentials.UserSession[0].user;

    const userData = {

    email , role , name ,

    referral : referralCode ,

    userId : _id

    }
  • 1
    @useVim Guess I'll use a screenshot instread..
  • 0
    @ElectroArchiver but then you are relying on the order of the properties in the user object?
    This sounds dangerous/unsafe for various reasons.

    Edit: Oh they are probably mapped by name. That’s why you create a second object afterwards.
  • 1
    @Lensflare JS has destructuring for arrays as well for objects, here with objects, the order doesn't matter.
  • 0
    @ElectroArchiver

    const [{ user }] = request.auth.credentials.UserSession;

    user.email

    user.role

    ...

    The TRICK!
  • 1
    @useVim Also if you're just renaming those 2 keys and the remaining stuff is all that's in user, you could do:

    const { referralCode : referral , _id : userId , ... args } = user;

    const userData = { referral , userId , ...args }
Add Comment