Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有可变维度的JavaScript函数数组_Javascript_Arrays - Fatal编程技术网

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

具有可变维度的JavaScript函数数组,javascript,arrays,Javascript,Arrays,我正在写一个函数,它在更高的维度上索引一个一维数组。用户可以选择索引的深度 例如 用户可以选择例如深度1。这意味着它将由字符串的第一个字符索引 index["a"] = ["Alaska", "Alabama"]; index["n"] = ["North Korea"]; 如果用户选择depth=2,则它将是索引为两个起始字符的2D数组 index["a"]["l"] = ["Alaska", "Alabama"]; 等等 如何实现一个可以在JavaScript中填充可变维度数组的函数 v

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

例如

用户可以选择例如深度1。这意味着它将由字符串的第一个字符索引

index["a"] = ["Alaska", "Alabama"];
index["n"] = ["North Korea"];
如果用户选择depth=2,则它将是索引为两个起始字符的2D数组

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

如何实现一个可以在JavaScript中填充可变维度数组的函数

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

问候语这似乎符合您的要求:

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;
}
函数createIndex(存储,深度){
var指数={};
深度=深度| | 1;
//在商店中的每个商品上循环
store.forEach(函数(项){
var lower=item.toLowerCase(),
当前=索引,
烧焦
//循环角色,构建存储结构
对于(i=0;i
此功能:

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

其中
index[“a”][“l”].\u items
index.a.l.\u items
可用于访问
[“阿拉斯加”、“阿拉巴马州”]

你打算付我多少钱来为你写这篇文章?这样的索引用于什么,即,你想解决什么问题?是否有理由反对简单地搜索数组?这个问题似乎离题了,因为您要求我们为您做这项工作。我正在考虑从最后一个数组变量temp[store[i][depth-1]]开始。push(store[i])。然后我需要将temp推入另一个Var temp2。索引用于通过数组查找ArrayLop中的特殊字符串。如果(第一个字符没有数组)创建数组。如果第二个字符没有数组,请创建一个数组。将单词推到数组中。这似乎更有用,尽管有点偏离了OPs的要求。OP的要求无论如何都无法满足。当然,除非您使用嵌套数组而不是对象,并在这些数组本身上定义一个字母的属性。这是一种不干净的做法。并且不会转换为JSON。好吧,OP从来没有要求在中间键处获得结果。别误会我的意思,我喜欢你所做的(+1 btw)。这不是我们想要的。。。OP selected()不提供中间键的结果这一事实支持了我的观点。啊,我明白你的意思了。嗯,是的,我想我应该马上建立一个完整的索引。不过,我不确定如此巨大的内存浪费是否真的有用。我的意思是,看看它用四个字的输入做了什么,想想它用4000字的输入做了什么。我很确定有一个更好的算法来做这类事情。因为OP并没有真正说明他首先想要实现什么,所以这只是基本递归的一个学术示例。
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"]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}