Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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,这是一个我用来检查输入是否与数组中的值相同以及是否有重复的脚本 echo '<input class="champion" type="text" list="champions" placeholder="Champion '.$i.'" name="champno[]" required autofocus><br/> <datalist id="champions"></datalist>';

这是一个我用来检查输入是否与数组中的值相同以及是否有重复的脚本

 echo '<input class="champion" type="text" list="champions" placeholder="Champion '.$i.'" name="champno[]" required autofocus><br/>
                        <datalist id="champions"></datalist>';
var config = {
              fillAll: true
            };

            document.forms["second_form"].oninput = function(e) {
              var champion = this["champno[]"];

              //Force into array
              if (champion[0] === undefined)
                champion = [this["champno[]"]];

              var reached = {
                valid: 0,
                unique: 0
              };

              var inputs = [].map.call(champion, function(n) {
                return n.value
              }).filter(function(n) {
                return n.length
              });

              var valid = [].every.call(champion, function(n, i) {
                n.setCustomValidity("");
                if (config.fillAll) return (reached.valid = i, champions.indexOf(n.value) > -1);
                else return n.value ? (
                  reached.valid = i,
                  champions.indexOf(n.value) > -1
                ) : true;
              });

              var unique = inputs.slice(0).sort().every(function(n, i, a) {
                reached.unique = inputs.lastIndexOf(n);
                return n != a[i - 1];
              });

              //Check for valid champions
              if (!valid) {
                champion[reached.valid].setCustomValidity("This is not a valid champion, please correct this field and resubmit.")
              }

              //Check for duplicates
              if (!unique) {
                champion[reached.unique].setCustomValidity("This champion has already been entered.")
              }

              this.checkValidity();
            };
为此:

var champions = {

    "Aatrox":["Blood Well","Dark Flight", "Blood Thirst / Blood Price", "Blades of Torment", "Massacre"],
    "Ahri":["Essence Theft","Orb of Deception", "Fox-Fire", "Charm", "Spirit Rush"],
    "Akali":["Twin Disciplines", "Mark of the Assassin", "Twilight Shroud", "Crescent Slash", "Shadow Dance"]
    };

这导致脚本出现错误:“champions.indexOf不是函数”,我不确定如何解决此问题。indexOf是数组原型的一部分,不可用于对象文本。如果您所做的只是检查密钥是否存在,则可以按如下方式执行:

if(冠军[n.value])…

或:


champions.hasOwnProperty(n.value)

它修复了我的大部分问题。代码应该检查输入是否与数组中的某个内容相同,因此用户必须键入Aatrox,例如aaaatrox,这不起作用,但检查重复项会起作用fine@Higeath通常情况下,在尝试检查该类型的内容时,您会“规范化”所检查的输入和值。删除空白,减少到所有小写字母,在极端情况下进行字符比较,以确定输入是否“足够接近”。它与常规数组配合得很好,所以我可以假设它仍然与code@Higeath我必须知道代码在哪里不起作用,将预期结果与现在的结果进行比较。如果更简单,为什么不简单地从对象的键重新创建旧数组,并保持所有代码不变?然后可以使用数组项引用对象值。
var champions = {

    "Aatrox":["Blood Well","Dark Flight", "Blood Thirst / Blood Price", "Blades of Torment", "Massacre"],
    "Ahri":["Essence Theft","Orb of Deception", "Fox-Fire", "Charm", "Spirit Rush"],
    "Akali":["Twin Disciplines", "Mark of the Assassin", "Twilight Shroud", "Crescent Slash", "Shadow Dance"]
    };