0

I have an object:

let obj = { rows: [1, 2] }

How can I do ES6 array destructuring with assignment for value 1?

For the array itself I can do like that, but I don't know how to get the first value:

let {rows: [first] } = obj

first // 1

Sorry, all is working in my example, was confused by error in my console.log test

4
  • 3
    b is 1 there, not [1,2]. Commented May 9, 2019 at 15:17
  • Format your question properly please. That's not even a valid object. Commented May 9, 2019 at 15:17
  • @jonrsharpe ehh, sory my mistake, didn't see second console.log in my code :/ Commented May 9, 2019 at 15:18
  • What output you want? Commented May 9, 2019 at 15:18

1 Answer 1

1

If you want to get first value of array then use the code below.

const data ={
 rows:[1,2,3]
}
let {rows:[first]} = data;
console.log(first)

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

Comments

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.