Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!