Unable to determine the size of the array being passed as a parameter in the function

When passing an array as a parameter to a function, I aim to retrieve its length.

Within my code, I am working with an array of objects.

groupList:group[]=[];

Within the selectItem function, I invoke the testExistinginArray function.

selectItem (event) {
    if(!this.toggle) {
        this.groupList.push(event);
    }
    else{
        this.ejList.push(event);
        if(!this.testExistinginArray(event,this.groupList)) { //I pass groupList as parameter
            this.groupList.push({code:event.codeBusinessGroup,name:event.nameBusinessGroup})
        }
    }
}

testExistinginArray(event,type: any[]) {
    for(let i=0;i<this.type.length;i++) {
        if (this.type[i].code.indexOf(event.codeBusinessGroup)===-1) {
            return false
        }
        else{
            return true
        }
    }
}

However, I encounter an error message indicating an undefined length.

TypeError: Cannot read property 'length' of undefined

Answer №1

Instead of accessing the length property of `this.type`, you should use `type.length` directly. In this case, `type` is an argument variable and not a function variable, so using `this` to read it is not applicable.

Answer №2

You attempted to retrieve the attribute length from this.type instead of from the function parameter type. It appears to be a typographical error.

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

Incorporating a switch button for toggling functionality in a Rails application

I attempted to convert a select tag into a JavaScript toggle on/off switch within my Rails application. After some searching, I came across a straightforward solution on the W3Schools website. http://www.w3schools.com/jquerymobile/tryit.asp?filename=tryj ...

Trouble generating document with Firebase setDoc()

In my current project, I am successfully creating a new user with authentication and then using the generated UID to create a new document. Here is the code snippet: const currentUser = await auth.createUserWithEmailAndPassword(email, pass); consol ...

Having trouble accessing and setting the values of a JSON array retrieved using $.get method in JavaScript outside of the function

Executing the following code snippet results in the desired values being displayed in console.log: var q; $.get('/ajax_subscribers', { code: 'qyhskkcnd'}, function(returnedData){ q = returne ...

Showcasing external API JSON data on Google Maps

My goal is to integrate remote API JSON data into Google Maps. Currently, my code successfully works with local JSON data that is within the same script. However, I want to populate the Google Map with remote API JSON data. I am using AngularJS Google Maps ...

Is it feasible to use Angular 2 in conjunction with local storage and JWT implementation on IE9 and

Could someone please advise me on how to implement local storage in Angular2 for IE9 and above? I've attempted following this guide https://medium.com/@blacksonic86/angular-2-authentication-revisited-611bf7373bf9#.h42z63t9v, but my Angular 2 applicati ...

Error in Angular 5: Cannot find property 'then' in type 'Observable<any>'

I encountered the following error message: "[ts] Property 'then' does not exist on type 'Observable'. How can I resolve this issue? Below is my Component code: getUsers(){ this.authService.getUsers().then((res) => { thi ...

Tips for documenting curried functions using Js docs

I'm encountering issues while trying to use Js docs for generating static documentation of my project. When attempting to run the js doc command, I am faced with numerous parse errors specifically in areas where I have curried functions. Strangely, my ...

Having trouble with GoogleSignIn in React Native Firebase app

During the development of my React Native App, I integrated Firebase as the Authentication System. The app compiled successfully, but upon attempting to use the GoogleSignIn method, an error message appeared on the AndroidToast: "Se ha producido un error. ...

JSLint error: Inconsistent use of spaces and tabs detected

I recently ran the code below through JSLint: $(document).ready(function() { /* Insert paragraph upon page load */ // Retrieve all header elements var header = document.getElementsByTagName('h1'), parent, ...

Deactivating Bootstrap Modal in Angular

Looking for advice on managing a Bootstrap Modal in Angular 7 I have a Form inside a Bootstrap Modal that I need to reset when the modal is closed (by clicking outside of it). Despite searching on Google, I haven't been able to find a solution. Any ...

Navigating to a specific router outlet in Angular programmatically - Tips for changing routes in a TypeScript file

I am encountering an issue with my side navigation and the named router outlet within it. Specifically, I am attempting to assign the "aside" named router outlet to a sub component called "top-words-aside", but it seems unable to locate the URL segment. r ...

The persistent issue persists with the Sails.js Node server where the req.session remains devoid of any data, despite utilizing

Currently, I have an Ember app running on http://localhost:4200 and a Sails app running on http://localhost:1337. In order to set up a pre-signup survey policy in my Sails application, I have the following code in /api/controllers/ProcessSurveyController. ...

Error message: "Unidentified variable in the code snippet from MUIv5 sample."

Achieving the Objective To implement a drawer sidebar in MUI5 that can be toggled open and closed by the user, I am exploring the documentation for the Drawer component as well as referencing an example. Encountering an Issue Upon copying the code from ...

I prefer the value to switch to false whenever I navigate to a new route and then return to the previous route, as the sidebar remains open

click here for image details view image description here Struggling to set the value as false when revisiting this site. Need assistance! Could someone lend a hand, please? ...

What are some strategies for troubleshooting asynchronous errors in JavaScript function calls?

I'm currently working on an asynchronous JavaScript code that utilizes the async method findDevices from the class Devices, which is located in a separate file. This method involves performing a mongo find operation within the IDevices collection. Her ...

Error: The property '...' is not found in the ReactElement<any, any> type, but it is required in the type '{...}'

As a beginner in TypeScript, I am currently working on rendering a page by fetching data from getStaticProps. The code snippet I am using for this purpose is: import React, {FormEvent, useState} from "react"; import { InferGetStaticPropsType } fr ...

What is the reason for having two plugin declarations within the galleriffic.js file?

I am currently working on enhancing the functionality of galleriffic.js by implementing a feature that will update a <div> element with text content as images are being changed. However, I am facing some challenges understanding the code. What perpl ...

Retrieve information from Angular 2 response

In my code, I am working with 1 component and 1 service. Here is the component element : ... constructor(private requestService : RequestService) { } ngOnInit() { this.a = this.requestService.send_request(urlWebAPI); console.log(this.a); } ... ...

Error: props.addPost does not exist as a function

Help Needed with React Error: props.addPost is not a function. I am trying to add a new post in my app. Please assist me. import React from "react"; import classes from "./MyPosts.module.css"; import { Post } from "./Post/Post.jsx& ...

What is the advantage of parsing values in a switch statement compared to an if condition?

function checkInput() { let value = document.getElementById('test').value; if (value == 1) { console.log("It works with if statement"); } } function checkSwitch() { let value = document.getElementById('test').value; switc ...