使用IndexOf的拆分数组不起作用

时间:2015-10-20 14:36:00

标签: javascript arrays list split indexof

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

问题是我有一个项目列表,我想从随机项目中拆分。我正在尝试“IndexOf()”,但它总是返回-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);

       }

1 个答案:

答案 0 :(得分:2)

您的问题是您获得的索引是一个整数,并检查数组中不存在的整数的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);

}

小提琴:http://jsfiddle.net/h2a4L67k/1/