-1

Which code should I use on js to map an array with spliting cells yet not reapeating ["12,3","3","5","66,22"] into ["12","3","5","66","22"].

1
  • This is not a place where you can ask us to write code for you. First tell us what you have tried and if you tried what issue you are facing. Please go through what kind of questions you can ask here Commented Apr 20, 2017 at 16:21

4 Answers 4

2

You could join and split the string.

console.log(["12,3", "3", "5", "66,22"].join().split(','));

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

Comments

1

I believe that you miss one 3 element in your desired output, if so - try following solution:

var arr = ["12,3","3","5","66,22"],
    res = [].concat(...arr.map(v => v.split(',')));
    
    console.log(res);

1 Comment

He says 'not reapeating' so he probably wants the result to be unique. I guess you could use a Set to do that easily res = Array.from([...new Set(res)])
1

You can use this ES6 way to get the desired output

x = ["12,3","3","5","66,22"];
y = [];
for (i of x){
  y = [...y, ...(i.split(","))]
}

Comments

0

To throw another onto the pile:

   a = Array.from([...new Set(["12,3", "3", "5", "66,22"].flatMap(x=>x.split(",")))])
console.log(a)

if uniqueness wasn't required, then just doing flatMap to the input would be enough

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.