From what I understood, you want to find the first following position of a repeated item in an array sequence.
Original Answer
This can be done with findIndex, which takes a callback to determine if the index counts as found. Here you can specify that the items need to equal and the index needs to be greater than your current index, thus it is a repeated item.
let newArray = ['t', 'r', 'c', 'g', 't', 'h'];
for (let i = 0; i < newArray.length; i++) {
// Find the index of the first item which index is greater than the current item and the latter equals the item
const x = newArray
.findIndex((item, index) => index > i && newArray[i] === item);
console.log(`${i}: ${x}`);
}
Better Solution
As mentioned in the comments (thanks T.J. Crowder), indexOf takes a second parameter as an offset to start the search at. This is the preferrable solution, as it faster and more concise.
let newArray = ['t', 'r', 'c', 'g', 't', 'h'];
for (let i = 0; i < newArray.length; i++) {
const x = newArray.indexOf(newArray[i], i+1);
console.log(`${i}: ${x}`);
}
newArray[i], instead. I'm not sure why you are doing this, though. It will just return the index of the first instance['t', 'r', 'c', 'g', 't', 'h']is supposed to end up as0:4, 1:-1, 2:-1, 3:-1, 4:-1, 5:-1