1

I have an array of arrays in TypesScript and I want to splice it to get the first element of each sub-array. This isn't that hard of a task but I want a concise way of doing it.

Here is a Python example of what I want in TypeScript:

l1 = [[1,2],[3,4],[5,6]]
l2 = [i[0] for i in l1]
print(l2) # [1, 3, 5]
1
  • 1
    Did you look at map() ? You want more concise than that? Commented Nov 8, 2022 at 18:43

1 Answer 1

2

You would use map to transform each sub-array, passing it a function that takes a sub-array and returns its first element:

const input = [[1, 2], [3, 4], [5, 6]]
const output = input.map(subarray => subarray[0])
console.log(output) // => [1, 3, 5]
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.