0

How do I use array destructuring instead of Array.slice()

Are there any benefits instead of slicing it?

transposed[idx] = [ 'Calendar Name', 'Standard' ]

acc.name = transposed[idx].slice(1)[0]; // => 'Standard'
1
  • 2
    what is transposed, idx? Commented Jun 15, 2021 at 11:53

3 Answers 3

1

You could destruct the array with [arg1,arg2]

const [a,b] = [ 'Calendar Name', 'Standard' ]

console.log(a,b)

for yours

   const [calName,standard] = transposed[idx]
Sign up to request clarification or add additional context in comments.

Comments

1
const [first,second] = transposed[idx];
// first = 'Calendar Name'
// second = 'Standard'

Honestly, the way to do this is transposed[idx][1], not array destructuring or slice if all you want is the value Standard, if you're just going to drop Calendar Name on the floor anyway and not use it

2 Comments

ignoring the first item (const [,second] = transposed[idx];) would also work.
@marzelin - very nice! It sure does. Personally, I find that hard on the eyes. Feels like the OP is doing a learning assignment or getting poor code review feedback to be honest - KISS.
1

Answer after editing the question

const arr = ["Calendar Name", "Standard"];

const [first, second] = arr;
console.log(first, second);

Answer before editing the question.

const arr = [
  ["Calendar Name", "Standard"],
  ["Valid From", 44197],
  ["Valid To", 44561],
  ["Use Holidays", "yes"],
  [
    "Working Day",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
  ],
  [
    "Start",
    0.3333333333333333,
    0.3333333333333333,
    0.3333333333333333,
    0.3333333333333333,
    0.3333333333333333,
    "-",
    "-",
  ],
  [
    "End",
    0.8333333333333334,
    0.8333333333333334,
    0.8333333333333334,
    0.8333333333333334,
    0.8333333333333334,
    "-",
    "-",
  ],
];

const [[first, second]] = arr;
console.log(first, second);

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.