In my thesaurus app, users can enter a base word like "dog" and its synonyms like "canine, hound, mutt." Once entered, these words are stored in a database. However, to streamline the process and avoid multiple form submissions, I want to create simultaneous entries for each synonym along with the base word. This will eliminate the need for users to manually repeat entries.
For example, with the given input, I aim to generate the following datasets:
var Keys =
[
dog,canine,mutt,hound
];
var Values = [
[mutt,canine,hound],[dog,mutt,hound],[canine,dog,hound],[dog,canine,mutt]
];
To achieve this, I plan to iterate through each key and retrieve the corresponding array of values from the Values dataset for insertion into the database. Unfortunately, my attempts at creating the nested loops required to generate this dataset have not been successful.
I have made the following attempt and welcome any suggestions for improvement:
var baseWord = [];
baseWord.push("dog");
var synonyms = ["hound","mutt","canine"];
var words = baseWord.concat(synonyms);
console.log(words.length); //outputs 4
//arrays to store inner loop results
var Keys = [];
var Values = [];
for(var i = 0; i < words.length; i++){
keys.push(words[i]);
for(var x = 0; x < words.length; x++){
var tempValues = words;
for(var o = 0; o < words.length; o++){
if(words[o] === words[x]){
tempValues.splice(tempValues[o]);
}
console.log(JSON.stringify(tempValues));
}
Values.push(tempValues);
};
};
console.log("the new keys array is :: %s", JSON.stringify(Keys)); //still getting 'dog'
console.log("the new values array is :: %s", JSON.stringify(Values)); //still getting [[]]