-1

Consider this code which creates a sequence of links to (not copies from) elements in the items array -

class Item { constructor(text) { this.text = text; }}
let items = [new Item ('a'), new Item ('b'), new Item ('c'), new Item ('a')]
let sequence = [items[0], items[3], items[1]]
console.log(sequence[2].text); //'b'

Let's prove that sequence is linked to (not copied from) elements in the items array -

items[1].text= 'bb'; 
console.log(sequence[2].text); //'bb'

From visual inspection of the code, we see that sequence[1] is linked to items[3].

console.log(sequence[1].text); //'a'

How could we programmatically determine which index of items that an element of sequence is linked to ? For example sequence[1] is linked to index 3 of the items array.

user2309803
  • 541
  • 5
  • 15
  • 1
    Loop over it (or use array functions for that), and compare using strict equality? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality#description: _"If both operands are objects, return true only if they refer to the same object."_ – CBroe Feb 28 '23 at 12:54
  • 1
    also [How to check if two vars have the same reference?](https://stackoverflow.com/questions/13685079/how-to-check-if-two-vars-have-the-same-reference) and [Get the index of the object inside an array, matching a condition](https://stackoverflow.com/questions/15997879/get-the-index-of-the-object-inside-an-array-matching-a-condition) – pilchard Feb 28 '23 at 13:04

2 Answers2

1

You can use array.findIndex to find the specific index.

class Item { constructor(text) { this.text = text; }}
let items = [new Item ('a'), new Item ('b'), new Item ('c'), new Item ('a')]
let sequence = [items[0], items[3], items[1]]

let currentItem = sequence[2];
let indexOfItems = items.findIndex(a => a === currentItem);
console.log('sequence[2] is items[' + indexOfItems + ']');
TKoL
  • 13,158
  • 3
  • 39
  • 73
  • flag as duplicate. – pilchard Feb 28 '23 at 12:56
  • 2
    @pilchard yes but here it uses `findIndex` to get there and it's adding something – Diego D Feb 28 '23 at 12:58
  • 1
    I tire of people with enough rep to know to flag to close not doing so. There are plenty of ['findIndex' duplicates](https://stackoverflow.com/questions/15997879/get-the-index-of-the-object-inside-an-array-matching-a-condition) and the condition is covered by the one I flagged. If every question had to be answered with specific syntax and variable naming to match the question this site would lose its meaning. ([The map and the territory](https://en.wikipedia.org/wiki/The_Map_and_the_Territory)) – pilchard Feb 28 '23 at 13:01
  • @pilchard I just meant to fix things the formal way...in this latest duplicate you shared, surely the accepted answer does the same as you pointed out before – Diego D Feb 28 '23 at 13:03
0

Using the strict equality operator === we can check whether two references refer to the same object.

sequence[1] === items[3] // true
sequence[0] === items[0] // true
sequence[0] === items[3] // false

So to know which sequence object refers to which items object, just iterate over both and compare them with the strict equality operator.

pilchard
  • 12,414
  • 5
  • 11
  • 23
Plagiatus
  • 360
  • 2
  • 12