1

How to split 2nd value in array?

Input [["ABCD","123,456"],["EFGH","565,565,878"]]

Required Output [["ABCD","123","456"],["EFGH","565","565","878"]]

I tried array.map(x => x[1].split(',')) but I am getting output as [["123","456"]],["565","565","878"]] 1st value is getting omitted. How to include 1st value also? If I use x.split(',') I am getting error

x.split is not a function

4
  • 1
    input.map(array => array.map(str => str.split(",")).flat()) Commented Aug 3, 2022 at 6:39
  • @ChrisG ok I see solutions are better in comments :) someone else says don't answer in comments. very confusing environment. Final story I had to remove my answer. Commented Aug 3, 2022 at 6:40
  • @DiegoDeVita 99.9% of questions here are dupes, typos or rtfm. I only post an answer if a) due diligence b) actually not answered before. But again, this never happens. So I downvote/flag the question but still help the OP with a comment (since they don't know their question is bad because SO removed the rules from the question form) Commented Aug 3, 2022 at 6:50
  • @DiegoDeVita Add to this that lots of high rep users apparently have decided to just F it and post answers to obvious dupes. SO has been going down the drain for years, all we can do to keep a minimum of quality control is to keep downvoting and flagging. Commented Aug 3, 2022 at 6:53

1 Answer 1

4

Try this

input.map(([name, list])=>[name, ...list.split(',')])
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.