使用 IndexOf 拆分数组不起作用

Split Array using IndexOf is not working

这是 link 我已经问过的另一个问题的问题(参见此处:Sort list items from random child)。

问题是我有一个项目列表,我想从一个随机项目中拆分出来。我正在尝试 "IndexOf()" 但它 returns 总是 -1。知道我做错了什么吗?

这是我的代码:

HTML

<ul class="myClass">
        <li class="item">
          <a href="URL" title="Company A" target="_blank">
            <span class="logo">
              <img src="images/logo1.jpg">
            </span>
          </a>
        </li>
        <li class="item">
          <a href="URL" title="Company B" target="_blank">
            <span class="logo">
              <img src="images/logo2.jpg">
            </span>
          </a>
        </li>
        <li class="item">
          <a href="URL" title="Company C" target="_blank">
            <span class="logo">
              <img src="images/logo3.jpg">
            </span>
          </a>
        </li>
        <li class="item">
          <a href="URL"  title="Company D" target="_blank">
            <span class="logo">
              <img src="images/logo4.jpg">
            </span>
          </a>
        </li>
        <li class="item">
          <a href="URL" title="Company E" target="_blank">
            <span class="logo">
              <img src="images/logo5.jpg">
            </span>
          </a>
        </li>
    ...
    </ul>

JAVASCRIPT

function randomizeChild(){
        var listItems = [];

        $('ul.myClass li').each( function() {
          listItems.push(this);   
        });

        var randomChild = Math.round(Math.random() * listItems.length);

        console.log("Random Child ---> " + randomChild);

        var indexToSplit = listItems.indexOf(randomChild);
        var first = listItems.slice(0, indexToSplit);
        var second = listItems.slice(indexToSplit + 1);

        console.log(indexToSplit); // it returns always -1 :(
        console.log(first, second);

       }

您的问题是您正在获取整数索引并检查数组中不存在的整数的 indexOf。你有一个元素数组。

http://jsfiddle.net/h2a4L67k/

试试这个:

function randomizeChild() {
    var listItems = [];

    $('ul.myClass li').each(function () {
        listItems.push(this);
    });

    var randomChild = Math.round(Math.random() * listItems.length);

    console.log("Random Child ---> " + randomChild);

    var randomChildObject = listItems[randomChild];
    var indexToSplit = listItems.indexOf(randomChildObject);
    var first = listItems.slice(0, indexToSplit);
    var second = listItems.slice(indexToSplit + 1);

    console.log(indexToSplit); // it returns always -1 :(
    console.log(first, second);

}

您甚至不需要执行上述操作。您已经有了一个随机索引,所以请立即使用它:

function randomizeChild() {
    var listItems = [];

    $('ul.myClass li').each(function () {
        listItems.push(this);
    });

    var randomIndex = Math.round(Math.random() * listItems.length);

    console.log("Random Child ---> " + randomIndex);

    var first = listItems.slice(0, randomIndex);
    var second = listItems.slice(randomIndex + 1);

    console.log(randomIndex); // it returns always -1 :(
    console.log(first, second);

}

Fiddle: http://jsfiddle.net/h2a4L67k/1/