dna配对,将阵列推入阵列中

时间:2015-05-05 19:15:56

标签: javascript arrays push

function pair(str) {

  var dna = [];
  var dnaarr = [];

    for(var i = 0; i < str.length; i++) {

      if(str[i].indexOf('G') === 0) {
        var a = dna.push('C');
      }
      if(str[i].indexOf('C') === 0) {
        var b = dna.push('G');
      }
      if(str[i].indexOf('A') === 0) {
        var c = dna.push('T');
      }
      if(str[i].indexOf('T') === 0) {
        var d = dna.push('A');
      }

    }
    for(var j = 0; j < str.length; j++) {
      var e = dnaarr.push(str[j]);
      var f = dnaarr.push(dna[j]);
    }

    return dnaarr;
}

pair("ATGCG");

当我运行此代码时,它返回

[ 'A', 'T', 'T', 'A', 'G', 'C', 'C', 'G', 'G', 'C' ]

我需要它返回

[['A', 'T'], ['T', 'A'], ['G', 'C'], ['C','G'], ['G', 'C']]

有人可以帮我解释一下这段代码吗?

7 个答案:

答案 0 :(得分:6)

这是一个更简单的版本:

function pair(str)
{
    // Array to hold the pairs
    var dna = [];
    // Loop through the string
    for (var i = 0; i < str.length; i++) {
        // Switch based on the current letter in the string
        // Push an array to dna with the current string and it's pair
        // in the case of 'G' the array would be ['G','C']
        // dna would then be [['G','C']]
        switch(str[i])
        {
            case "G":
                dna.push([str[i],"C"]);
                break;
            case "C":
                dna.push([str[i],"G"]);
                break;
            case "A":
                dna.push([str[i],"T"]);
                break;
            case "T":
                dna.push([str[i],"A"]);
                break;
        };
    }
    // return the array
    return dna;
}

pair("ATGCG")

答案 1 :(得分:1)

你的数组推送出了问题。

function pair(str) {

 
  var dnaarr = [];
  //var dnatot = [];

    for(var i = 0; i < str.length; i++) {
         var dna = [];
dna.push(str[i]); //pushing current str[i]
      if(str[i].indexOf('G') === 0) {
        var a = dna.push('C');
      }
      if(str[i].indexOf('C') === 0) {
        var b = dna.push('G');
      }
      if(str[i].indexOf('A') === 0) {
        var c = dna.push('T');
      }
      if(str[i].indexOf('T') === 0) {
        var d = dna.push('A');
      }
dnaarr.push(dna); //pushing the array dna to the main array dnaarr
    }
   

    return dnaarr;
}

console.log(pair("ATGCG"));

答案 2 :(得分:1)

我相信这很容易阅读和理解。

function pairElement(str) {
  var pairs = { 'G': 'C', 'C': 'G', 'A': 'T', 'T': 'A' };

  return str.split('').map(function(char) {
    return [char, pairs[char]];
  });
}

pairElement("GCG"); // [['G', 'C'], ['C', 'G'], ['G', 'C']]

编辑:代码已简化。在函数的开头,我们使用可能的字符对创建对象。使用 split map 数组方法,我们将str中的每个char与对象对配对。

答案 3 :(得分:0)

我发现这种方法最具可读性。评论中的解释

function pairElement(str) {
  // base pairs defined by proj requirements
  var basePairs = {"A": "T", "T": "A", "C": "G", "G": "C"};
  var newGeneCodeArr = [];
  for (var i = 0; i < str.length; i++) {
    // build individual sequence pair that fits requirements
    var newGeneCode = [];
    newGeneCode.push(str[i]);
    newGeneCode.push(basePairs[str[i]]);
    newGeneCodeArr.push(newGeneCode);
  }
  return newGeneCodeArr;
}

答案 4 :(得分:0)

function pairElement(str) {
  //convert the string into array of characters
  str = str.split('');
  //define a multidimensional array to hold pairing
  var arr = [];
  //using the for loop, we can check for each character
  for(var i=0; i<str.length; i++)
    {
      var tmp = [];
      //checking characters and adding pairs inside a tmp array
      switch(str[i]){
        case 'G':
          tmp.push(str[i]);
          tmp.push('C');
          break;
        case 'C':
          tmp.push(str[i]);
          tmp.push('G');
          break;
        case 'T':
          tmp.push(str[i]);
          tmp.push('A');
          break;
        case 'A':
          tmp.push(str[i]);
          tmp.push('T');
          break;
      }

      //if tmp has something inside, add it to the multidimensional array
      if(tmp.length > 0)
        arr.push(tmp);
    }

  return arr;
}


pairElement("GCG");

答案 5 :(得分:-1)

另一个,可能更简单,更清洁的版本:

function pair(str) {
  var arr = str.split(''),
      newArr = [];

  arr.forEach(function(e){

    if (e == 'A') {
      newArr.push([e, 'T']);
    } else if (e == 'T') {
      newArr.push([e, 'A']);
    } else if (e == 'G') {
      newArr.push([e, 'C']);
    } else {
      newArr.push([e, 'G']);
    }

  }); 

  return newArr;
}

示例:

pair('GATC');

将返回:

[['G','C'], ['A','T'], ['T', 'A'], ['C', 'G']] 

答案 6 :(得分:-1)

function pair(str) {
  // object of dna paring key, value
  var pairing = {
        "A": "T",
        "T": "A",
        "C": "G",
        "G": "C"
      },
      // array to store paired object
      final = [];
  // make an array by .split and map that array by using the dna paring object
  final = str.split('').map(function(val) {
    var arr = [];
    arr.push(val, pairing[val]);
    return arr;
  });

  // return the array of paired objects
  return final;
}