-1

Here is what I want:

I have first array and I have the index as below:

const first = ['a','b','c'];
const index = 1; // the index of b

const shuffled = ['c','a','b']; // now the index of b is 2 

In the first array the index points to the second element in the array which is b right?

Now I shuffled the first array and we have the shuffled array .

The index of b is 2 right now. I want to return this new index...

Sara Ree
  • 3,417
  • 12
  • 48
  • shuffled.indexOf('b'); – Stefan Avramovic Oct 01 '21 at 18:37
  • 1
    Does this answer your question? [Difference Between indexOf and findIndex function of array](https://stackoverflow.com/questions/41443029/difference-between-indexof-and-findindex-function-of-array) – JDB Oct 01 '21 at 18:41

3 Answers3

3

Using Array#indexOf:

const first = ['a','b','c'];
const index = 1;
const shuffled = ['c','a','b'];

const target = first[index];
const newIndex = shuffled.indexOf(target);

console.log(newIndex);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
1

you can use shuffled.indexOf(first[index])

first[index] returns the value of the first index in this case 'b'

shuffled.indexOf('b') would return the index of b in the shuffled array

K i
  • 539
  • 5
  • 12
1

You can use,

Let value = first[index]

let newValue =shuffled.indexof(value)