Skip to content
Advertisement

Array.prototype.find() – Error: Cannot read properties of undefined (reading x)

I have a Typescript project in which I have an array of objects from which I want to extract the value of a key where another value matches the local variable.

I need to get the value of sheetId of the object where the value of title is equal to the value of fileName

This is my object:

JavaScript

This is the variable:

JavaScript

This is what I want to get:

JavaScript

This is what I am doing, but I only get it to show me true in the object that matches, but I don’t know how to extract the key:

JavaScript

Update:

This is what I’m doing to find the value of sheetId:

JavaScript

This is the error it shows:

JavaScript

My problem: how can I control that undefined or how can I assign it a 0, for example so that it has a value

Advertisement

Answer

You need to use .find method to find and extract your value with dot notation.

JavaScript
Advertisement