简体   繁体   English

如何找到 Arrays 内部的差异值并记录差异的索引

[英]How to Find the Differences Values Inside Arrays and Log the Index of the Differences

Happy New Year All!祝大家新年快乐!

But Sorry i want to ask a question, I have an array like this但是对不起我想问一个问题,我有一个这样的数组

[ 'even', 'even', 'odd', 'even', 'even' ]

I want the output will said 3, because the differences values inside of array is 'odd' and it laid in third index (the index is start from 1)我想要 output 会说 3,因为数组内部的差异值是“奇数”并且它位于第三个索引中(索引从 1 开始)

And also, when the question is changed into而且,当问题变成

[ 'odd', 'even', 'odd', 'odd', 'odd' ]

The output will get 2, because the differences values inside the array is 'even' and it laid in second index output 将得到 2,因为数组内的差异值是“偶数”并且它位于第二个索引中

Is there anyone could help me?有没有人可以帮助我?


I have tried using javaScript method map, for loop, and other,... but i don't know, how to get the differences values of an array and print the indexed from it我尝试过使用 javaScript 方法 map、for 循环和其他,...但我不知道如何获取数组的差异值并从中打印索引

Thanks谢谢

You can use findIndex on the main array and check if the element is unique by comparing its index and lastIndex您可以在主数组上使用findIndex并通过比较其索引和 lastIndex 来检查元素是否唯一

 const findIndexOfUnique = (arr) => arr.findIndex(x => arr.indexOf(x) === arr.lastIndexOf(x)) + 1 console.log(findIndexOfUnique([ 'even', 'even', 'odd', 'even', 'even' ]))

Heading:标题:

indexOf method looks for an element from start to end. indexOf方法从头到尾查找一个元素。 And lastIndexOf does the same from end to start. lastIndexOf都是一样的。 So if element is unique then both will result same otherwise they will be different.因此,如果元素是唯一的,那么两者的结果将相同,否则它们将不同。

findIndex() take a function as an argument and returns the index of the first element which satisfies the condition. findIndex()将 function 作为参数并返回满足条件的第一个元素的索引 So actually we are saying that.所以实际上我们是这么说的。 Find the first index of the element which matches the condition arr.indexOf(x) === arr.lastIndexOf(x) .找到与条件arr.indexOf(x) === arr.lastIndexOf(x)匹配的元素的第一个索引。 Then we add 1 to get the required output然后我们加1得到需要的output

I can't comment yet but wanted to do so on Maheers answer, if you change all of the array entries to 'even' it returns 0 instead of -1, since you're adding one.我还不能发表评论,但想在 Maheers 的回答中这样做,如果您将所有数组条目更改为“偶数”,它会返回 0 而不是 -1,因为您要添加一个。

Best to update it:最好更新一下:

function findIndexOfUnique(arr){
    let uniqueIndex = arr.findIndex(x => arr.indexOf(x) === arr.lastIndexOf(x));
    if (uniqueIndex >= 0) uniqueIndex++;
    return uniqueIndex;
}

console.log(findIndexOfUnique([ 'even', 'even', 'odd', 'even', 'even' ])) // 3
console.log(findIndexOfUnique([ 'even', 'even', 'even', 'even', 'even' ])) // -1

However, what if there are multiple unique entries?但是,如果有多个唯一条目怎么办? In this case it would simply return the first unique entry.在这种情况下,它只会返回第一个唯一条目。 Example:例子:

[ 'even', 'even', 'even', 'odd', 'even', 'three', 'even' ]

Should it return [ 4, 6 ]?它应该返回 [ 4, 6 ] 吗?

function findUniqueIndexes(arr) {
  let uniqueIndexes = [];
  for (let index = 0; index < arr.length; index++) {
    if (index  === arr.lastIndexOf(arr[index]))
      uniqueIndexes.push(index + 1);
    }
  }
  return uniqueIndexes
}

And if you want to expand this further:如果您想进一步扩展:

function findUniqueIndexes(arr) {
  let uniqueIndexes = [];
  for (let index = 0; index < arr.length; index++) {
    if (index  === arr.lastIndexOf(arr[index]))
      uniqueIndexes.push({"index" : index + 1, value: arr[index]});
    }
  }
  return uniqueIndexes
}

This last one would return:最后一个将返回:

[{ index: 4, value: 'odd' }, { index: 6, value: 'three' }]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM