gpt4 book ai didi

javascript - 将数组中的字符串分解为子数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:12:32 25 4
gpt4 key购买 nike

进行 DNA 挑战,如此接近但显然误解了 prototype.split("")。将这些字符串 ["AC", "CA", "TA"] 转换为子数组的最佳方法是什么? [["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"));

最佳答案

你可以使用.mapsplit它们由 ""

var o =  ["AC", "CA", "TA"];

var s = o.map(e=> e.split(""));

console.log(s)

关于javascript - 将数组中的字符串分解为子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50183312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com