Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an identity index value in JavaScript?

In JavaScript, values of objects and arrays can be indexed like the following: objOrArray[index]. Is there an identity "index" value?

In other words:

Is there a value of x that makes the following always true?

let a = [1, 2, 3, 4];
/* Is this true? */ a[x] == a

let b = { a: 1, b: 2, c: 3 };
/* Is this true? */ b[x] == b

Definition of an identity in this context: https://en.wikipedia.org/wiki/Identity_function

like image 524
Shivam Avatar asked Oct 05 '21 01:10

Shivam


People also ask

What is index value in JavaScript?

Definition and Usage. The indexOf() method returns the first index (position) of a specified value. The indexOf() method returns -1 if the value is not found. The indexOf() method starts at a specified index and searches from left to right. By default the search starts at the first element and ends at the last.

Can you index in JavaScript?

Introduction to the JavaScript array indexOf() methodTo find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.

How can I get the index of an object by its property in JavaScript?

To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.

Do objects have indexes JavaScript?

In JavaScript, arrays use numbered indexes. In JavaScript, objects use named indexes.


2 Answers

There is no such thing built-in, as there is rarely a need for it (and sometimes even a need against it).0 It is nevertheless possible to roll your own ‘identity’ key:

const self = Symbol('self');

Object.defineProperty(Object.prototype, self, {
    enumerable: false,
    get() { "use strict"; return this; }
});

This will work on all primitives (other that null and undefined) and most JavaScript objects: that is, other than proxies or those that bypass the usual prototype chain by means of e.g. Object.create(null). Any object later in the prototype chain will also be able to disable the functionality, e.g. by doing { [self]: void 0 }; all these caveats mean that x[self] === x is by no means a universal law. But this is probably the best you can do.

Modifying Object.prototype is usually considered a bad idea, but the above manages to avoid most of its badness: adding the property at a symbol key (and making it explicitly non-enumerable as well) prevents it from unexpectedly showing up in iterations and lookups that walk the prototype chain, helping ensure no code should be impacted that does not specifically look for this property.


0 Even if such a feature existed, it would not be a good solution for the asker’s original use case: a ‘cut to 50 characters or take the whole string if shorter’ operation can be expressed as s.description.substring(0, s.description.length > 50 ? 50 : void 0) (or in fact just s.description.substring(0, 50)). It wouldn’t be any easier to express even with such a feature: depending on the condition, you still need to invoke the substring method, not just look it up, but not invoke the ‘self’ non-method. And given that you need to append an ellipsis at the end in the former case, you would still have to perform the condition check outside the substring call, making any shorthand rather ineffective. All that said, tricks like described in this answer do find some real use.

like image 141
user3840170 Avatar answered Oct 14 '22 03:10

user3840170


The indexing operation doesn't have an identity element. The domain and range of indexing is not necessarily the same -- the domain is arrays and objects, but the range is any type of object, since array elements and object properties can hold any type. If you have an array of integers, the domain is Array, while the range is Integer, so it's not possible for there to be an identity. a[x] will always be an integer, which can never be equal to the array itself.

And even if you have an array of arrays, there's no reason to expect any of the elements to be a reference to the array itself. It's possible to create self-referential arrays like this, but most are not. And even if it is, the self-reference could be in any index, so there's no unique identity value.

like image 26
Barmar Avatar answered Oct 14 '22 04:10

Barmar