I have a thesaurus app, so the user may enter "dog" as a base word, and "canine,hound,mutt" as the synonyms. These are then added to a database. Along with adding "dog" as the base word, i'd like to make multiple simultaneous entries for each of the synonyms, so that the user doesn't have to go back and enter "hound" followed by "dog,canine,mutt". This would involve multiple form submissions.
Based on the above example, I want to create the following datasets before heading to the database:
var Keys =
[
dog,canine,mutt,hound
];
var Values = [
[mutt,canine,hound],[dog,mutt,hound],[canine,dog,hound],[dog,canine,mutt]
];
Once I have this, I can do a simple loop through each key and grab the corresponding array within Values, and carry out my inserts. For example, when iterating based on their length, I would grab index 2 and get a key of 'mutt' and then grab the values of '[canine,dog,hound]'. So far, my attempts to perform the required nested loops to achieve this dataset haven't proved fruitful.
So far, i've tried this, and am hopeful of some suggestions:
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 [[]]