gpt4 book ai didi

javascript - 创建键数组和值数组数组

转载 作者:行者123 更新时间:2023-11-30 09:30:57 24 4
gpt4 key购买 nike

我有一个词库应用程序,所以用户可以输入“dog”作为基本词,输入“canine,hound,mutt”作为同义词。然后将这些添加到数据库中。除了添加“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”的键,然后获取“[canine,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 [[]]

最佳答案

尝试这样的事情:

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

关于javascript - 创建键数组和值数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46333692/

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