1

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.

5
  • 3
    detail should be an object, not an array. Commented Oct 8, 2021 at 2:12
  • 1
    Arrays can have named properties, since they're also objects, but they're not shown by default in the console. Commented Oct 8, 2021 at 2:12
  • The object you store in the array is stored in a property of the array (cat2 in this case; which is why the array is still of length 0). Using array property is generally hacky; you should really use an object instead. Commented Oct 8, 2021 at 2:14
  • This might depend on the environment you run on. Chrome dev console and node.js do print everything out. Commented Oct 8, 2021 at 2:24
  • Thanks for the answers. Now I know I should use Object and changing the detail to detail = {}; works. I was just curious why the array wasn't working even with a position fulfilled. Commented Oct 8, 2021 at 3:14

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.