JavaScript negative element in array - javascript

This is my JavaScript array code for negative index number.
In output why doesn't consider negative index number in the count of the element? It shows only count (3) in output.
Code
let abc = ['gnagar', 'ahmedabad', 25];
console.log(abc, typeof(abc));
console.log(abc[-1]);
abc[-1] = 'abc';
console.log(abc, typeof(abc));
console.log(abc[-1]);

-1 is not a valid index for array.
The assignment abc[-1] = 'abc'; means set attribute "-1" to the abc object.

This is because array is type of object as you can see there typeof(abc) is object.
You can assign values in objects using [].
Negative indexes are not actual index so it does not impact the array length.

Related

Javascript array of json prints empty but it has an object inside

I'm trying to understand why Javascript prints an empty array when it should have at least one value inside it. Please, check this code:
detail = [];
detail.cat1=[];
detail.cat2=[];
detail.cat3=[];
detail.cat4=[];
var newEntry = {"cod":"01","dt":"2021-10-02 09:07:21.205-07:00"};
detail.cat2.push(newEntry);
console.log(detail);
console.log(detail.length);
console.log(detail.cat2);
The results are:
> Array []
> 0
> Array [Object { cod: "01", dt: "2021-10-02 09:07:21.205-07:00" }]
How could it print [] since I have one object inside? And how can the length be zero? I was using any Javascript online editor, and the result is the same.
Since an array is really an object, we can add methods/properties directly to an individual array.
Then if you want to see the keys in an object for instance, Object.keys won't see it, but Reflect.ownKeys() will.
The Reflect version of defineProperty will return success or fail, while Object's returns the object passed.
Remember Reflect.preventExtensions() will (like it says) stop you or others from extending them.
As far as question about length is there, ".length" gives length of values not properties so it still gives 0
Array is an indexed collection of values, means you can put element at particular index and get element from particular index.
In the below example, you can see that if there are already elements in an array then you can get its length using length property.
Remember: The length of an array is (highest integer index at which the element is present in array + 1)
const arr = [1, 2, 3, 4];
console.log(arr); // prints whole array
console.log(arr.length); // prints length of an array
console.log(arr[2]); // Element at index 2
If you set element at particular index let say arr[8] = "something" then its length will be 9
const arr = [];
arr[8] = "something";
console.log(arr.length) // 9
It is true that array's are objects, so you can set/get values to/from array. But if you set property that is not a numbered index then it will not increase its length but It will set the property to that array object as:
const arr = [];
arr["introduction"] = "I'm an array";
arr.property = "This is a property"
console.log(arr.length);
console.log(arr.introduction);
console.log(arr.property);
When displaying an array, the default Array.prototype.toString method is usually called. This actually calls Array.prototype.join with no arguments, so produces a comma separated string of the elements of the array.
The join method only returns properties that have a positive integer name (an array index, indicating an array element), other properties are ignored by join.
Similarly, only elements of the array affect the length property, ordinarly properties (i.e. those that don't have an array index for their name) don't affect length.

Trying to get Array length

See below given scenario
var x = [];
x['abc'] = "hello";
console.log(x.length); //returns 0
var x = [1,2];
x['abc'] = "hello";
console.log(x.length); //returns 2
Any reason behind this or Am i missing anything?
As per spec
The value of the length property is numerically greater than the name
of every own property whose name is an array index; whenever an
own property of an Array object is created or changed, other
properties are adjusted as necessary to maintain this invariant.
Further
Specifically, whenever an own property is added whose name is an array
index, the value of the length property is changed.
So, length property is adjusted when the property whose name is an array index is created, only then length property changes.
'abc' being a non-numeric (and hence not an index of an array) when added to the array, doesn't reflect any change in length property.
By writing
var x = [ ];
you are defining a standard array where indexes can only be integers.
x['abc'] = "hello";
When you do x["abc"] since abc (which is what you use as index) is not an integer you are basically defining a property to the arr object (everything is object in javascript) but you are not adding an element to the standard array. and hence you array remains as it was.
var x = [];
x["abc"] = "hello";
console.log(x);
and that is the reason the size of the array is 0 in the first case and 2 in the second
When you wish to add property to x with string keys, you need to define you variable as an object.
var x = {}
x['abc'] = "hello";
and then you can get the size of you Object(which is indeed the number of keys in your object) by
Object.keys(x).length
you cannot access the array without the index. If you want to insert particular data in an array you can give it like,
var x = [];
x[0] = "hello";
alert(x.length) //will return 1
What you have done is that you have converted your array into an associative array, where your indices are the reference to the value assigned. This is similar to the objects concept in Javascript.
The length is 0 because a real array starts from index 0. So if you check for your array
console.log(x[0]) //undefined
hence, the length 0.
You cannot append a user defined key that isn't a number in JavaScript, as you can do in Python or Php etc. In JavaScript the key's of the Array shall only be numeric.
Thus doing:
var x = [];
x['abc'] = "hello";
console.log(x.length);
returns the length of x as 0. As nothing has been pushed into the array.If you do console.log(x), you'll find out that the array has no values or is empty.
Whereas doing this:
var x = [1, 2];
x['abc'] = "hello";
console.log(x.length); //returns 2
returns the length as 0 because the array consists of number 1 at the index 0 and the number 2 at the index/position 1 of that array. Just those values are there in the array, if you want to push "hello" push it like, x.push("hello") which will push it at the index 2 of the array.
If you still want to use alphabetic index use Objects instead.

Why javascript shows 0 length for array with object property instead of indexed element

For Example:
var arr = [];
arr[3.4] = 1;
console.log(arr.length);
In the above code sample the length property holds zero why and what happened inside JS parser because it's length is zero.
An array's length is reflective of the largest array index present in the array. An "array index" (see below) is a property name that is an integer value less than 232−1. Since 3.4 is not an integer, setting it does not alter the length.
Arrays are objects, so there is no reason why an array can't have a property named 3.4, but that property doesn't influence the length because its name does not fit the criteria of an array index.
ES2015 9.4.2, Array Exotic Objects defines an "array index" as an integer less than 232−1:
A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232−1."
And that definition is used in relation to the length value (emphasis mine):
Every Array object has a length property whose value is always a nonnegative integer less than 232. The value of the length property is numerically greater than the name of every own property whose name is an array index; whenever an own property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever an own property is added whose name is an array index, the value of the length property is changed, if necessary, to be one more than the numeric value of that array index...
A JavaScript array can not have fractional indexes.
What you have done there is assigned a property called "3.4" on the array object.
This does not affect the length property, which is designed to return one number higher than the highest valid index.
If you think about how arrays are supposed to work, you should realise a fractional offset doesn't make sense.
You could try using an object vs an array.
var arr = {};
arr[3.4] = 1;
arr[3.5] = 2;
arr[3.6] = 3;
console.log(Object.keys(arr).length);
You must use an integer as an index, not a float.
var arr = [];
arr[3] = 1;
console.log(arr.length);

Javascript - array index issue in non sparse array

non sparse array are contiguous in nature and from 0 to length-1, all index should return true for 'in' operator. It should be true also for empty element.
obj1 = {x:1,y:2};
obj2 = Object.create(obj1);
obj2.z = 5;
var arr = [obj1,obj2,1,,2];
console.log(arr.length); //5
console.log(3 in arr);//false
index 3 is valid.
Why is it returning false?
Note: I am using latest firefox.
It should return false only for sparse array which doesn't have a specified index number 3.
Update: index 3 is valid so it should return true. in operator doesn't validate value of at the indexed position, it just validate the validity of index not the value
3 in arr is false because arr[3] is never set (and thus the array is indeed sparse).
Note the double comma in
var arr = [obj1,obj2,1,,2];
– if you make that
var arr = [obj1,obj2,1,2];
then 3 in arr becomes true.
See page 63 in the specification:
Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the
element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another
comma), the missing array element contributes to the length of the Array and increases the index of
subsequent elements. Elided array elements are not defined. If an element is elided at the end of an array,
that element does not contribute to the length of the Array.
– in other words, the behavior of your code is identical to
var arr = [obj1, obj2, 1];
arr[4] = 2;
Javascript is evaluating the value which is undefined. Undefined is falsy.
Array always have 0 based index, in your array 4th element (3 by index) is not set to any value.
Refere this
From the link
Here is what is falsy in JavaScript:
false
null
undefined
The empty string
''
The number 0 The number NaN (yep, 'Not a Number' is a number, it is a special number)
Everything else is truthy, and that includes Infinity (which is
another special number, like NaN), and all Object objects and Array
objects, empty or not.

Javascript array length incorrect on array of objects

Could someone explain this (strange) behavior? Why is the length in the first example 3 and not 2, and most importantly, why is the length in the second example 0? As long as the keys are numerical, length works. When they are not, length is 0. How can I get the correct length from the second example? Thank you.
a = [];
a["1"] = {"string1":"string","string2":"string"};
a["2"] = {"string1":"string","string2":"string"};
alert(a.length); // returns 3
b = [];
b["key1"] = {"string1":"string","string2":"string"};
b["key2"] = {"string1":"string","string2":"string"};
alert(b.length); // returns 0
One thing to note is that there is a difference between regular arrays and associative arrays. In regular arrays (real arrays), the index has to be an integer. On the other hand, associative arrays can use strings as an index. You can think of associative arrays as a map if you like. Now, also note, true arrays always start from zero. Thus in your example, you created an array in the following manner:
a = [];
a["1"] = {"string1":"string","string2":"string"};
a["2"] = {"string1":"string","string2":"string"}
Javascript was able to convert your string indexes into numbers, hence, your code above becomes:
a = [];
a[1] = {"blah"};
a[2] = {"blah"};
But remember what i said earlier: True arrays start from zero. Therefore, the javascript interpreter automatically assigned a[0] to the undefined. Try it out in either firebug or the chrome/safari console, and you will see something like this when you try to print "a". You should get something like "[undefined, Object, Object]. Hence the size 3 not 2 as you expected.
In your second example, i am pretty sure you are trying to simulate the use of an associated array, which essentially is adding properties to an object. Remember associated arrays enable you to use strings as a key. So in other terms, you are adding a property to the object. So in your example:
b["key1"] = {"string1":"string","string2":"string"};
this really means:
b.key1 = {"string1":"string","string2":"string"};
Initializing b =[] simply creates an array, but your assignment doesn't populate the array. It simply gives "b" extra properties.
length returns 1 + the largest integer key in the object.
In a the largest key is 2 so 1+2 is 3.
In b there are no integer keys (the keys there are key1 and key2 which cannot be converted into ints) so Javascript assumes that the largest key is -1, and 1 + -1 yields 0.
This program will help you see that:
a = [];
a["1"] = {};
a["4"] = {};
alert(a.length); // Prints 5
From the ECMAScript standard, ECMA-262, 5th ed.
15.4.5.2 length
The length property of this Array object is a data property whose value is always numerically greater than the name of every deletable property whose name is an array index.
Note the length property of an array only takes into account array indices, which are integers; setting other properties doesn't affect length.
For an array, a["3"] is equivalent to a[3] (this behavior is specified by § 15.4.5.1); 3 is an array index rather than a property. Thus setting a["3"] affects the array's length. b["key1"] is equivalent to b.key1. Setting properties don't affect the length of a collection.

Categories