Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda