1

I have a word and I want to retrieve a array of aliases that I make for that word. For example I'll have an array ["hello", "hi", "hey", "yo"]. If my word is "hey", then I want to be able to get that entire array. My first idea was to use objects like so:

let aliasdict = {
   "hello": ["hello", "hi", "hey", "yo"],
   "hi": ["hello", "hi", "hey", "yo],
   "hey": ["hello", "hi", "hey", "yo"],
   "yo": ["hello", "hi", "hey", "yo"],
}

The catch is that I plan to have over 100 different phrases each with 2-4 different aliases. So it'll be something like this:

let aliasdict = {
   "hello": ["hello", "hi", "hey", "yo"],
   "hi": ["hello", "hi", "hey", "yo],
   "hey": ["hello", "hi", "hey", "yo"],
   "yo": ["hello", "hi", "hey", "yo"],

   "blue": ["blue", "green", "white"],
   "green": ["blue", "green", "white"],
   "white": ["blue", "green", "white"],

   "head": ["head", "knees", "tail"],
   ...
   ...
}

The amount of copy-pasting needed leaves me with some distaste so I want to ask if there is a simpler solution without copy-pasting so much and without detracting from the speed of using dictionaries.

Thanks.

1
  • You would need to structure the array list in a far simpler method. Just place the word and all aliases in a sub-array element of the array and lookup the match and return either the array sub or the balance of the elements in it. Commented May 27, 2020 at 4:44

4 Answers 4

2

Alias

const alias = (list = []) => list.reduce((group, alias, index, aliases) => ({
   ...group,
   [alias]: aliases,
}), {});

Dictionary

const dictionary = (...lists) => lists.reduce((dictionary, list) => ({
   ...dictionary,
   ...alias(list),
}), {});

Implement

dictionary(
  ["hello", "hi", "hey", "yo"],
  ["goodbye", "by", "bye", "piece"]
);

Output

{
  "hello": [
    "hello",
    "hi",
    "hey",
    "yo"
  ],
  "hi": [
    "hello",
    "hi",
    "hey",
    "yo"
  ],
  "hey": [
    "hello",
    "hi",
    "hey",
    "yo"
  ],
  "yo": [
    "hello",
    "hi",
    "hey",
    "yo"
  ],
  "goodbye": [
    "goodbye",
    "by",
    "bye",
    "piece"
  ],
  "by": [
    "goodbye",
    "by",
    "bye",
    "piece"
  ],
  "bye": [
    "goodbye",
    "by",
    "bye",
    "piece"
  ],
  "piece": [
    "goodbye",
    "by",
    "bye",
    "piece"
  ]
}

Time Concerns You'd have to have a pretty large list, or an application that really cares about speed for this amount of processing to have any noticeable negative effect on your application.

That being said, if your dictionary does become large enough to effect the rest of your application you could run these functions and copy the output so you're not resolving the output at runtime.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this seems like what I wanted, although it took some googling for me to understand what was going on. For reference, my lists won't be long enough that doing this kind pre-generation would affect my Node app.
@secretlyrice reduce is a highly under used javascript function :) Probably the most important one out there. Using reduce, you are able to re-create many js array methods. From reduce, you can create - Array map - Array filter - Array every - Array some - etc... Personally, I utilize reduce much more than any other function and all but omit using forEach anymore.
0

You can view each word as a node on a graph. Then for each linked word pair, you are forming a connection between two nodes.

You can thus use an Adjacency List to form your data structure.

The words associated with one another will have an edge on this graph. Looking a node up in such a map will have O(1) lookup time.

https://www.geeksforgeeks.org/implementation-graph-javascript/

Comments

0

You can use method with a switch statement if using an object is not a restriction.

function getAlias(key) {
    switch(key) {
       case "hello":
       case "hey":
       case "yo":
          return ["hello", "hi", "hey", "yo"];

       case "blue":
       case "green":
           return ["blue", "green"];
    }
}

Comments

0

There are many ways and better ways to do it. But here is one method.

function getAliases(find,array) {
 for (let i in array) {
  if (array[i].findIndex(element => element==find)>-1) return array[i];}
 return "";  
}

//=============================
let aliasdic = [
  ["hello", "hi", "hey", "yo"],
  ["blue", "green", "white"],
  ["head", "knees", "tail","back"]
  ];

console.log(getAliases("hey"  ,aliasdic));    // Array ["hello", "hi", "hey", "yo"]
console.log(getAliases("green",aliasdic));    //Array ["blue", "green", "white"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.