Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
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:

let list = [
  {
    "properties": {
      "sheetId": 1000297558,
      "title": "ser"
    }
  },
  {
    "properties": {
      "sheetId": 24134863,
      "title": "atr"
    }
  },
  {
    "properties": {
      "sheetId": 668935915,
      "title": "work"
    }
  }
]

This is the variable:

let fileName = 'atr'

This is what I want to get:

let testId = 24134863

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:

let sheetFile = list.map((elem: any) => elem.properties.title == fileName)

Update:

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

let sheetId: number = list.find((elem: any) => elem.properties.title == fileName).properties.sheetId

This is the error it shows:

Error: Cannot read properties of undefined (reading 'properties')

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

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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

const fileName = 'atr'
const list = [
  {
    "properties": {
      "sheetId": 1000297558,
      "title": "ser"
    }
  },
  {
    "properties": {
      "sheetId": 24134863,
      "title": "atr"
    }
  },
  {
    "properties": {
      "sheetId": 668935915,
      "title": "work"
    }
  }
]

const result = list.find((item) => item.properties.title === fileName).properties.sheetId;

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

First, you have to filter to find matches.

After that, extract (with map) from the previous matches.

let list = [
  {
    "properties": {
      "sheetId": 1000297558,
      "title": "ser"
    }
  },
  {
    "properties": {
      "sheetId": 24134863,
      "title": "atr"
    }
  },
  {
    "properties": {
      "sheetId": 668935915,
      "title": "work"
    }
  }
]

let fileName = 'atr'

let testId = 24134863

let sheetFile = list
  .filter((elem) => elem.properties.title == fileName)
  .map((elem) => elem.properties.sheetId)
 
console.log(sheetFile)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda