简体   繁体   中英

Breaking strings in an array into subarrays

Doing the DNA challenge and so close but clearly misunderstanding prototype.split("") . What's the best way to turn these strings ["AC", "CA", "TA"] into subarrays? [["A","C"]["C","A"]["T","A"]]

 function pairElement(str) { //break into array var arr = str.split(""); //add new letter (could be refactored as switch) for (i = 0; i < arr.length; i++) { if (arr[i] == "G") { arr[i] += "C"; } else if (arr[i] == "C") { arr[i] += "G"; } else if (arr[i] == "T") { arr[i] += "A"; } else if (arr[i] == "A") { arr[i] += "T"; } } //break into arrays again //this is how I'm trying to use.split to break it up. Doesn't work. var broken = []; for (x = 0; x < arr.length; x++) { broken += arr[x].split(""); } //return return arr; } console.log(pairElement("GCG")); 

you can use .map and split them by ""

 var o = ["AC", "CA", "TA"]; var s = o.map(e=> e.split("")); console.log(s) 

You actually have to just push the split result in the broken array and return it !

 function pairElement(str) { //break into array var arr = str.split(""); //add new letter (could be refactored as switch) for (i = 0; i < arr.length; i++) { if (arr[i] == "G") { arr[i] += "C"; } else if (arr[i] == "C") { arr[i] += "G"; } else if (arr[i] == "T") { arr[i] += "A"; } else if (arr[i] == "A") { arr[i] += "T"; } } //break into arrays again //this is how I'm trying to use.split to break it up. Doesn't work. var broken = []; for (x = 0; x < arr.length; x++) { broken.push(arr[x].split("")); } //return return broken; } console.log(pairElement("GCG")); 

要回答“什么是最好的方式”问题,请将数组映射到自身的拆分版本中:

const subarrays = array.map(pair => pair.split());

Very simple in functional style:

> seq = ['AC', 'CA', 'TA']
[ 'AC', 'CA', 'TA' ]
> seq.map(s => s.split(''))
[ [ 'A', 'C' ], [ 'C', 'A' ], [ 'T', 'A' ] ]

Overall, I'd do some refactoring on the whole function:

 var m = new Map([["G", "C"], ["C", "G"], ["A", "T"], ["T", "A"]]); function pairElement(str) { return [...str].map(c => [c, m.get(c)]); } console.log(pairElement("GCG")); 


And if there's a guarantee that the sub-arrays are never mutated, then you can save a good bit of memory by reusing arrays instead of creating them over and over.

 var m = new Map([["G", ["G", "C"]], ["C", ["C", "G"]], ["A", ["A", "T"]], ["T", ["T", "A"]]]); function pairElement(str) { return [...str].map(c => m.get(c)); } console.log(pairElement("GCG")); 


But to directly answer your question, you can do it without explicit .split() calls. Since you know there's always two characters, you can use parameter destructuring on the strings.

 var arr = ["AC", "CA", "TA"]; var s = arr.map(([a, b]) => [a, b]); console.log(s) 


Or even a little shorter using rest syntax, like this:

 var arr = ["AC", "CA", "TA"]; var s = arr.map(([...a]) => a); console.log(s) 


Or using spread syntax in an array literal:

 var arr = ["AC", "CA", "TA"]; var s = arr.map(s => [...s]); console.log(s) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM