Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建键数组和值数组数组_Javascript_Arrays_Dataset_Nested Loops - Fatal编程技术网

Javascript 创建键数组和值数组数组

Javascript 创建键数组和值数组数组,javascript,arrays,dataset,nested-loops,Javascript,Arrays,Dataset,Nested Loops,我有一个同义词库应用程序,所以用户可以输入“dog”作为基本单词,输入“犬科、猎犬、杂种”作为同义词。 然后将它们添加到数据库中。 除了添加“dog”作为基本单词外,我还想为每个同义词同时输入多个条目,这样用户就不必返回并输入“hound”,然后再输入“dog,cange,mutt”。这将涉及多种形式的提交 基于上述示例,我希望在前往数据库之前创建以下数据集: var Keys = [ dog,canine,mutt,hound ]; var Values = [ [mutt

我有一个同义词库应用程序,所以用户可以输入“dog”作为基本单词,输入“犬科、猎犬、杂种”作为同义词。 然后将它们添加到数据库中。 除了添加“dog”作为基本单词外,我还想为每个同义词同时输入多个条目,这样用户就不必返回并输入“hound”,然后再输入“dog,cange,mutt”。这将涉及多种形式的提交

基于上述示例,我希望在前往数据库之前创建以下数据集:

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

var Values = [
    [mutt,canine,hound],[dog,mutt,hound],[canine,dog,hound],[dog,canine,mutt]
];
一旦我有了这个,我可以通过每个键做一个简单的循环,获取值中相应的数组,并执行插入。例如,当根据它们的长度进行迭代时,我会获取索引2并获取一个键'mutt',然后获取值'[cange,dog,hound]'。 到目前为止,我尝试执行所需的嵌套循环以实现此数据集,但没有取得成效

到目前为止,我已经尝试过这一点,并希望得到一些建议:

    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 [[]]
var baseWord=[];
基本词汇。推(“狗”);
var同义词=[“猎犬”、“杂种狗”、“犬科动物”];
var words=基词.concat(同义词);
console.log(words.length)//产出4
//用于保存内部循环计算结果的新数组
var键=[];
var值=[];
for(var i=0;i
这里有一个简单的循环来构建输出

外循环用于键,内循环构建一个集合,该集合被推送到值数组中

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

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

外循环用于键,内循环构建一个集合,该集合被推送到值数组中

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

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

1.这个

var tempValues = words;
words
是一个数组。这意味着它通过引用传递。这意味着
tempValue
words
,您对
tempValue
所做的任何编辑都将对
words
进行。这就引出了下一点:

2.您使用的拼接错误

tempValues.splice(tempValues[o]);
这意味着:

tempValues.splice("dog");
在第一次通过该循环时。不幸的是,Array.splice没有将字符串作为第一个参数。它使用索引。MDN不会记录传递字符串时它所做的操作。但它的行为就像是将0放入一个数组


.splice(0)
意味着从第0个索引开始删除数组中的所有内容。因此,在通过临时数组的第一个循环中,它会将所有内容剥离,然后不再循环(因为没有任何内容可以循环)。因此,临时数组变为[].

看起来@Scuzzy对如何做有一个答案。我会让你知道你做错了什么

1.这个

var tempValues = words;
words
是一个数组。这意味着它通过引用传递。这意味着
tempValue
words
,您对
tempValue
所做的任何编辑都将对
words
进行。这就引出了下一点:

2.您使用的拼接错误

tempValues.splice(tempValues[o]);
这意味着:

tempValues.splice("dog");
在第一次通过该循环时。不幸的是,Array.splice没有将字符串作为第一个参数。它使用索引。MDN不会记录传递字符串时它所做的操作。但它的行为就像是将0放入一个数组


.splice(0)
意味着从第0个索引开始删除数组中的所有内容。因此,在通过临时数组的第一个循环中,它会将所有内容剥离,然后不再循环(因为没有任何内容可以循环)。因此,临时数组变为[]。

请尝试以下操作:

//操作码
var baseWord=[];
基本词汇。推(“狗”);
var同义词=[“猎犬”、“杂种狗”、“犬科动物”];
var words=基词.concat(同义词);
console.log(words.length);//输出4
//和新代码
//将结果放入对象中
var字典={};
for(变量i=0,w;w=words[i];+i){
//记下每个单词(w)
字典[w]=单词。过滤器(函数(单词){
返回单词!=w;//除w以外的所有单词
});
}
//拿这个东西
console.log(字典);
//或者把它串起来
log(JSON.stringify(dictionary));
//只是为了行动
console.log(“直接应答”);
var keys=words.map(函数(word){
返回词;
});
console.log('Keys::'+JSON.stringify(Keys));//与“words”相同
var值=words.map(函数(word){
返回单词。过滤器(函数(w){
返回单词!=w;//除w以外的所有单词
});
});
log('Values::'+JSON.stringify(Values));
//ES6风格
console.log(“ES6风格”);
变量键