javascript - 查找数组集合中没有重复项的元素的索引

标签 javascript arrays sorting

我这里收集了一些数据,你可以看下面。 我想要的是获取在数组中具有唯一值的元素索引

var setArray = [ false, true, false, false ]
// Sample result will be 1, the index of unique value in array is true
// and has a index of 1
// code here to get the index...

我该如何解决这个问题?

最佳答案

var setArray = [ false, true, false, false ]

function singles( array ) {
        for( var index = 0, single = []; index < array.length; index++ ) {
            if( array.indexOf( array[index], array.indexOf( array[index] ) + 1 ) == -1 ) single.push( index );    
        };
        return single;
    };

singles(setArray); //This will return 1

ThinkingStiff 在 this 上稍作修改的函数问题以满足您的需求。只需传入您的数组,它就会返回唯一元素的索引值!就这么简单。让我知道进展如何。

关于javascript - 查找数组集合中没有重复项的元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588333/

相关文章:

javascript - 获取表单中当前选定的选项

java - 从 ArrayList 到数组的对象

PHP在多维数组中添加元素

python - 按排序顺序通过键遍历 Python 字典

javascript - Vanilla JS - 通过 div 的名称获取元素

javascript - Phonegap android 4.1 致命信号 11 而在 2.2 中工作正常

javascript - 通过 jquery 循环更新子范围内容

java - 如何将方法输出传递到数组元素中?

python - numpy 中的数组按行排序

javascript - 除数字外,如何按字母顺序对对象数组进行排序?