2

I have the following list:

nested_list = [
  ["[63]"],
  ["[61]"],
  ["[7]"],
  ["[63]"],
  ["[80, 18]"],
  ["[80, 43, 18, 20]"]
]

Which is a mess I know, but this is the data that I have to deal with at the moment and I need it to be like this:

[63, 61, 7, 63, 80, 18, 80, 43, 18, 20]

So basically converting it into a list of numbers. Is this doable using as minimum loops as possible?

I have used flat() nested_list .flat(2) and ended up with the following result:

[ '[63]', '[61]', '[7]', '[63]', '[80, 18]', '[80, 43, 18, 20]' ]

Also tried reduce + concat following this but since it is a string, it is not working and I'm not sure where to go from here.

0

1 Answer 1

8

.flatMap() + JSON.parse() can do this quite easily

const nested_list = [
  ["[63]"],
  ["[61]"],
  ["[7]"],
  ["[63]"],
  ["[80, 18]"],
  ["[80, 43, 18, 20]"]
];

const result = nested_list.flatMap(JSON.parse);

console.log(result)

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

1 Comment

Thanks a lot! This is amazing! Learning tons here :)

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.