代码之家  ›  专栏  ›  技术社区  ›  Oisín Foley

创建键数组和值数组

  •  1
  • Oisín Foley  · 技术社区  · 7 年前

    然后将其添加到数据库中。 除了添加“dog”作为基词外,我还想为每个同义词同时输入多个条目,这样用户就不必返回并输入“hound”,然后输入“dog,canine,mutt”。这将涉及多个表单提交。

    var Keys = 
    [
        dog,canine,mutt,hound
    ];
    
    var Values = [
        [mutt,canine,hound],[dog,mutt,hound],[canine,dog,hound],[dog,canine,mutt]
    ];
    

    一旦我有了这个,我可以通过每个键做一个简单的循环,获取值中相应的数组,然后执行插入。例如,当基于其长度进行迭代时,我会获取索引2并获取一个键“mutt”,然后获取“[犬科,犬科,猎犬]”的值。 到目前为止,我尝试执行所需的嵌套循环来实现此数据集,但没有取得成果。

        var baseWord = [];
        baseWord.push("dog");
        var synonyms = ["hound","mutt","canine"];
        var words = baseWord.concat(synonyms);                  
        console.log(words.length); //outputs 4
    
        //new arrays to hold the result of the inner loop calculation
        var Keys = [];
        var Values = [];
        for(var i = 0; i < words.length; i++){
            //removing the inner loops makes this output, the 4 values from the 'words' variable. with the inclusion of the loops, we only get back 'dog'
            keys.push(words[i]);
    
    
            for(var x = 0; x < words.length; x++){
                //I want to loop through the 'words' variable, and if current index of 'words' matches a value in tempValues(matches a different value of tempValues each time, which is what i'd want(it allows us to ignore the current key, and keep the values), then remove that particular value from tempValues, then push the remaining values in tempValues into our 'VAlues' array that we declared ouside all of these loops
                var tempValues = words;
                //console.log("tempvalues is :: %s", JSON.stringify(tempValues));
                    for(var o = 0; o < words.length; o++){
                        if(words[o] === words[x]){
                            //get rid of the value in tempValues that is equals to the value of the key in the outer loop(for this particular iteration)
                            tempValues.splice(tempValues[o]);
                        }
                        console.log(JSON.stringify(tempValues));
                    }
                    Values.push(tempValues);
            };
        };
        console.log("the new keys array is :: %s", JSON.stringify(Keys)); //keep getting dog
        console.log("the new values array is :: %s", JSON.stringify(Values)); //keep getting [[]]
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Alex Kudryashev    7 年前

    尝试以下操作:

    //OP code
    var baseWord = [];
    baseWord.push("dog");
    var synonyms = ["hound", "mutt", "canine"];
    var words = baseWord.concat(synonyms);
    console.log(words.length); //outputs 4
    
    //and new code
    //put result into an object
    var dictionary = {};
    for (var i = 0, w; w = words[i]; ++i) {
      //take each word (w)
      dictionary[w] = words.filter(function(word) {
        return word != w; //all words except w
      });
    }
    //take the object
    console.log(dictionary);
    //or stringify it
    console.log(JSON.stringify(dictionary));
    
    //Just for OP
    console.log('direct answer');
    var keys = words.map(function(word) {
      return word;
    });
    console.log('Keys :: ' + JSON.stringify(keys));//same as "words"
    
    var values = words.map(function(word) {
      return words.filter(function(w) {
        return word != w; //all words except w
      });
    });
    console.log('Values :: ' + JSON.stringify(values));
    
    //ES6 style
    console.log('ES6 style');
    var keys = words.map(word => {
      return word;
    });
    console.log('Keys :: ' + JSON.stringify(keys));//same as "words"
    
    var values = words.map(word => {
      return words.filter(w => {
        return word != w; //all words except w
      });
    });
    console.log('Values :: ' + JSON.stringify(values));
    
    //or even All In One
    console.log('All In One');
    var keyValues = words.map(word => {
    return [word, words.filter(w => {return word != w;})];
    });
    console.log(JSON.stringify(keyValues));
        2
  •  1
  •   Scuzzy    7 年前

    这里有一个简单的循环来构建输出。

    var BaseWord = 'dog';
    var Synonyms = ['canine','mutt','hound'];
    var Keys = Synonyms;
    var Values = [];
    
    Keys.unshift( BaseWord ); // add baseword to the start of Keys
    
    for( var i = 0; i < Keys.length; i++ )
    {
      var Set = [];
      for( var j = 0; j < Keys.length; j++ )
      {
        if( Keys[j] !== Keys[i] )
        {
          Set.push( Keys[j] );
        }
      }
      Values.push( Set );
    }
    
    console.log("the new keys array is :: %s", JSON.stringify( Keys ) );
    console.log("the new Values array is :: %s", JSON.stringify( Values ) );
    

    下面是我在PHP中的实现方法

    $BaseWord = 'dog';
    $Synonyms = array('dog','canine','mutt','hound');
    
    $keys = array( $BaseWord ) + $Synonyms;
    $values = array();
    
    foreach( $keys as $key )
    {
      $values[] = array_values( array_diff( $keys, array( $key ) ) );
    }
    
    echo json_encode( $keys );
    echo json_encode( $values );
    
        3
  •  1
  •   jlogan    7 年前

    看起来@Scuzzy有一个关于如何做到这一点的答案。我会让你知道你做错了什么。

    1

    var tempValues = words;
    

    words tempValue ,以及您对 温度值 将完成 . 这让我想到下一点:

    2.您使用的拼接错误

    tempValues.splice(tempValues[o]);
    

    tempValues.splice("dog");
    

    第一次通过这个循环。不幸的是,数组。splice不将字符串作为第一个参数。它需要一个索引。MDN不会记录在传递字符串时所做的操作。但它的行为就像你把一个0。

    .splice(0) 意味着从第0个索引开始从数组中删除所有内容。所以,在通过临时数组的第一个循环中,它会将所有内容都撕掉,然后不再循环(因为已经没有什么可以循环了)。所以,tempArray变成了[]。