SepGuest
SepGuest

Reputation: 127

JavaScript Function Array with variable dimensions

I am writing a function which is indexing a 1D Array in more dimension. The user can choose the depth of the index.

eg.

var store = ["Alaska", "Alabama", "North Korea", "South Korea"];

the user can choose for example depth 1. It means it will be index by the first char of the string

index["a"] = ["Alaska", "Alabama"];
index["n"] = ["North Korea"];

if the user chooses depth = 2 than it will be 2D Array with index the two beginning chars

index["a"]["l"] = ["Alaska", "Alabama"];

etc.

How do I accomplish a function which can populate variable Dimension arrays in JavaScript?

var indexthis = function(store,depth)
{
  if(depth == "undefined")
  {depth =1;}
//any Suggestions to go on?
};

Greetings

Upvotes: 2

Views: 182

Answers (2)

Tomalak
Tomalak

Reputation: 338406

This function:

function indexWord(index, word, letters) {
    var letter;
    if (!letters) letters = word.split("");
    while (letters.length) {
        letter = letters.shift().toLowerCase();
        if (!(letter in index)) index[letter] = {_items: []};
        index[letter]._items.push(word);
        indexWord(index[letter], word, letters);
    }
}

called like this:

var store = ["Alaska", "Alabama", "North Korea", "South Korea"],
    index = {}, i;

for (i = 0; i < store.length; i++) {
    indexWord(index, store[i]);
}

gives you the following index:

{
  a: {
    _items: ["Alaska", "Alabama"],
    l: {
      _items: ["Alaska", "Alabama"],
      a: {
        _items: ["Alaska", "Alabama"],
        s: {
          _items: ["Alaska"],
          k: {
            _items: ["Alaska"],
            a: {
              _items: ["Alaska"]
            }
          }
        },
        b: {
          _items: ["Alabama"],
          a: {
            _items: ["Alabama"],
            m: {
              _items: ["Alabama"],
              a: {
                _items: ["Alabama"]
              }
            }
          }
        }
      }
    }
  },
  n: {
    _items: ["North Korea"],
    o: {
      _items: ["North Korea"],
      r: {
        _items: ["North Korea"],
        t: {
          _items: ["North Korea"],
          h: {
            _items: ["North Korea"],
            " ": {
              _items: ["North Korea"],
              k: {
                _items: ["North Korea"],
                o: {
                  _items: ["North Korea"],
                  r: {
                    _items: ["North Korea"],
                    e: {
                      _items: ["North Korea"],
                      a: {
                        _items: ["North Korea"]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  s: {
    _items: ["South Korea"],
    o: {
      _items: ["South Korea"],
      u: {
        _items: ["South Korea"],
        t: {
          _items: ["South Korea"],
          h: {
            _items: ["South Korea"],
            " ": {
              _items: ["South Korea"],
              k: {
                _items: ["South Korea"],
                o: {
                  _items: ["South Korea"],
                  r: {
                    _items: ["South Korea"],
                    e: {
                      _items: ["South Korea"],
                      a: {
                        _items: ["South Korea"]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

where either index["a"]["l"]._items or index.a.l._items can be used to access ["Alaska", "Alabama"].

Upvotes: 1

cmbuckley
cmbuckley

Reputation: 42547

This seems to meet your requirements:

function createIndex(store, depth) {
    var index = {}; 
    depth = depth || 1;

    // loop over each item in the store
    store.forEach(function (item) {
        var lower = item.toLowerCase(),
            current = index,
            char;

        // loop over the characters, building the store structure
        for (i = 0; i < depth; i++) {
            char = lower.substr(i, 1); 

            // add the key, if it doesn't exist
            if (!current[char]) {
                current[char] = (i == (depth - 1) ? [] : {});
            }   

            // update the object reference
            current = current[char];
        }   

        // now add the item
        current.push(item);
    }); 

    return index;
}

Upvotes: 0

Related Questions