我这里收集了一些数据,你可以看下面。
我想要的是获取在数组中具有唯一值的元素
的索引
。
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/