创建数组键值和数组数组

问题描述:

我有一个同义词库应用程序,因此用户可以输入“dog”作为基本单词,而“canine,hound,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 [[]] 
+0

是您的PHP代码重要? – Scuzzy

+0

不,但这是除了阵列以外唯一的其他建议,我认为这将有助于暴露我认为是一个常见问题。 – OisinFoley

下面是一个简单的循环来构建输出。

外环是你的钥匙,而内环建立一组推到你的价值观阵列

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

欢呼声Scuzzy。虽然Alex's是更有用的解决方案,但您的答案输出与问题完美匹配。 – OisinFoley

外貌就像@Scuzzy有一个关于如何去做的答案。我会让你知道你做错了什么。

1.本

var tempValues = words; 

words是一个数组。这意味着它通过引用传递。这意味着tempValueISwords,并且您对tempValue所做的任何编辑都将完成到words。这使我下一个问题:

2.您使用的拼接错误

tempValues.splice(tempValues[o]); 

这相当于:

tempValues.splice("dog"); 

在第一次通过这个循环。不幸的是,Array.splice不会将字符串作为第一个参数。它需要一个索引。如果字符串通过,MDN不会记录它的作用。但它的行为就像放置了一个0.

.splice(0)表示从第0个索引开始的数组中删除所有内容。所以,在你通过临时数组的第一个循环中,它将所有东西都删除,然后不再循环(因为没有剩下的东西可以循环)。所以,tempArray变成[]。

+0

感谢jlogan,我想我需要重新熟悉splice的行为 – OisinFoley

尝试这样:

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

+0

伟大的答案亚历克斯。这是我将要使用的解决方案,但我认为我必须将接受的答案提供给Scuzzy,因为他的解决方案回答了问题的具体内容,即填充“Keys”和“Values”变量。 – OisinFoley

+0

@OisinFoley看看更新的答案。无需切换接受的答案。只要看看现代JS可以做什么;)。 –