[removed] how to map array and split string content to new cell

后端 未结 4 1862
遇见更好的自我
遇见更好的自我 2021-01-29 11:02

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\"]<

相关标签:
4条回答
  • 2021-01-29 11:12

    You could join and split the string.

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

    0 讨论(0)
  • 2021-01-29 11:17

    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);

    0 讨论(0)
  • 2021-01-29 11:24

    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(","))]
    }
    
    0 讨论(0)
  • 2021-01-29 11:24

    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

    0 讨论(0)
提交回复
热议问题