具有可变维度的JavaScript函数数组

时间:2014-07-09 13:00:04

标签: javascript arrays

我正在编写一个函数,它在更多维度上索引一维数组。用户可以选择索引的深度。

例如

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

用户可以选择例如深度1.这意味着它将通过字符串

的第一个字符串进行索引
index["a"] = ["Alaska", "Alabama"];
index["n"] = ["North Korea"];

如果用户选择深度= 2,那么它将是2D数组,索引是两个开始的字符

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

如何完成一个可以在JavaScript中填充变量Dimension数组的函数?

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

问候

2 个答案:

答案 0 :(得分:1)

此功能:

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

这样叫:

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

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

为您提供以下索引:

{
  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"]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

其中index["a"]["l"]._itemsindex.a.l._items可用于访问["Alaska", "Alabama"]

答案 1 :(得分:0)

这似乎符合您的要求:

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;
}