How come my array is suddenly masquerading as an object?

Within my code, I am working with a data object that has numerical keys and arrays as values containing objects. There comes a point where I need to retrieve a specific array from the object based on its key.

The structure of the object is similar to this:

this.data = {
    1: [{name: "John Doe", occupation: "farmer"}, {name: "Jane Doe", occupation: "teacher"}],
    3: [{name: "Jack Doe", occupation: "plumber"}, {name: "Jean Doe", occupation: "hairdresser"}]
}

In order to extract the arrays, I utilize the following snippet...

this.people = this.data[1];

Upon logging this.data, it displays {1: Array(2), 3: Array(2)}. When I log this.people, it shows (2) [{…}, {…}].

However, the output of typeof(this.people) confuses me as it returns object. What could be causing this issue?

Answer №1

Arrays in JavaScript are considered objects, as is the case with most data structures. To determine if a variable is an array object, you can utilize the Array.isArray method.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Button in Angular gets stuck when a touchscreen is long pressed

In my Angular2 application, I am facing an issue with a button when running on a Windows 10 touchscreen PC in Chrome. Normally, the button works fine and executes the click function. However, if the button is held for 1-2 seconds, it gets stuck and fails t ...

What is the best way to compare an array with comma-separated values in JavaScript?

I have a scenario where I have two arrays, one for categories and the other for products. Each product contains multiple categories as a comma-separated string. My goal is to match a specific category with each product's product_category value and the ...

Retrieving a time series data set from a JSON stream

Even though ThingSpeak offers great charts, I'm interested in retrieving data from ThingSpeak and creating my own visualizations using Google Charts. When extracting a "feed" from ThingSpeak, the data is presented in a JSON object like the one below: ...

What is the process of adding byte data in C++?

Is there a way to combine the eight values of a byte array uint8_t[8] into a single variable uint64_t? uint8_t array[8] = { 0xe4, 0x52, 0xcb, 0xbe, 0xa4, 0x63, 0x95, 0x8f }; uint64_t result = 0; for(int i = 0; i < sizeof(array); ++i) { // Any ...

Challenges encountered when implementing a personal library in a separate project

After updating a library I own, I seem to have encountered an issue when trying to use it in another project. However, the reason for this problem eludes me. A multitude of error logs with a similar theme are appearing: ERROR in ./node_modules/@company-na ...

TypeORM Error: Trying to access property 'findOne' of an undefined object

I've been working on implementing TypeORM with Typescript, and I have encountered an issue while trying to create a service that extends the TypeORM repository: export class UserService extends Repository<User> { // ... other service methods ...

Merge associative arrays with corresponding key value pairs across multiple dimensions

[data] => Array ( [0] => Array ( [id] => 1 [users] => 310 [views] => 1333 [date_added] => 2016-03-09 ) [1] => Array ...

Can we use a switch statement instead of having multiple @Input()s in Angular?

When passing an attribute into my Angular component like this: <my-component myAttribute></my-component> I aim to modify a variable within the component that controls the CSS properties for width and height. To simplify, I have predefined at ...

Construct an outdated angular project from scratch

I'm facing an issue with an old Angular project that I'm trying to build. After pulling down the code, running npm install @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc9f9095bccdd2cbd2c8">[email p ...

Utilizing a callback function to retrieve a list of elements from a NASA API

Here is my code snippet for fetching a single item from the NASA API using a completion handler, inspired by examples from an Apple Programming book. class PhotoInfoController { func fetchPhotoInfo(completion: @escaping (PhotoInfo?) -> Void) { ...

methods for obtaining access in JavaScript when dealing with an ArrayList<Object> that has been converted to a JSONArray

I am dealing with an Object ArrayList that contains various variables, stored in an ArrayList of Objects. I then convert it to a JSONArray and pass it to a JSP page. How can I display these objects in a textarea on the JSP page using JavaScript? public cl ...

Testing a function in Angular using Karma and Jasmine

I am facing an issue while testing a method in Angular using Jasmine/Karma. The error message I keep encountering is: TypeError: undefined is not iterable (cannot read property Symbol (Symbol.iterator)) This is how I created the method: myMethod(l ...

Having trouble getting my specialized pipe (filter) to function properly in Angular 2

I have implemented a custom pipe for filtering data in my table. Oddly, when I enter a search string into the input box, it correctly prints 'found' in the console but no rows are displayed in the table. However, if I remove the pipe altogether, ...

Troubleshooting Angular 2 Typescript: Component not displaying as expected

I am currently in the process of learning Angular 2. Despite encountering numerous error messages, I have successfully set up the routes and can now display the desired component. My next challenge is to incorporate a navbar component into my homepage comp ...

Steps for utilizing response data as parameters for useInfiniteQueryHere is how you can make

On the specific page where I am implementing useInfiniteQuery const { data, error, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage, status } = useInfiniteQuery( ['posts', searchState], ({ pageParam = 1 }) => post.search({ . ...

Arranging the navigation categories alphabetically in Magento 1.7

Can anyone help with changing the order in which sub categories are displayed on the left sidebar of a parent category page? For example, on the 'Brands' page, the subcategory names are currently listed in order of ID, rather than alphabetically ...

Error: The object 'exports' is not defined in geotiff.js at line 3

Looking to integrate the geotiff library with Angular 6.1.0 and TypeScript 2.9.2. Installed it using npm i geotiff Encountering the following error in the browser console: Uncaught ReferenceError: exports is not defined at geotiff.js:3 After r ...

A generic function in Typescript that can accept an object with the first argument as a specified key

I am looking to create a function that will take a string as its first argument and will only accept a second argument of type object if it contains the first argument as a key with a boolean value: const checkFlag = (str:string, obj) => obj[str] Alth ...

Using SAS to create a 2D array for tallying the number of null values in string variables within

In my dataset, I have 10 observations with 6 character variables representing days and cities. Each variable contains either the name of the day or the value 'NA' if the day is missing. Similarly, for city variables, they either have city names o ...

Error message in Angular 2: "__generator is not recognized"

I've been working on intercepting outgoing HTTP requests in Angular 2 in order to generate a token from the request body and attach it to the header of each post request. Below is the code snippet that I've implemented. Initially, I encountered ...