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

125
Visualizações
How to access array item by property value and then update it's value?

I have an array of object that looks like this:

const tickets = 
[
  {
    ticketId: 'aaa',
    authorId: 'abc',
    replyCount: 0,
    status: 'Open',
    threads: [
      {
        threadId: 'abc',
        authorId: 'abc',
        direction: 'in',
        content: 'blah blah blahh'
      },
    ],
  },
  {
    ticketId: 'bbb',
    authorId: 'efg',
    replyCount: 0,
    status: 'Open',
    threads: [
      {
        threadId: 'efg',
        authorId: 'efg',
        direction: 'in',
        content: 'blah blah blahh'
      },
    ],
  },
  .......
]

Now I want to access the array item where ticketId equals aaa and change it's threads property.

I have tried doing using tickets['aaa'].threads = [ ... ] but it throws these errors:

Eslint: Unsafe member access .threads on an 'any' value

TypeScript: Element implicitly has an 'any' type because index expression is not of type 'number'

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Find the index where ticketId equals "aaa"

tickets[tickets.findIndex(v => v.ticketId === "aaa")].threads = [/* ... */];

With index validation:

const idx = tickets.findIndex(v => v.ticketId === "aaa");
if (idx > -1) tickets[idx].threads = [/* ... */];
about 4 years ago · Juan Pablo Isaza Relatório

0

If you simply wanna mutate by property you can get the index with indexOf and mutate

tickets[tickets.findIndex(ticket => ticket.ticketId === "aaa")].threads = [] //mutate here however you want
about 4 years ago · Juan Pablo Isaza Relatório

0

I hope the following is useful for you.

var getItem = id => tickets.find(item => item.ticketId === id);

getItem("aaa"); // {ticketId: 'aaa', authorId: 'abc', replyCount: 0, status: 'Open', threads: Array(1)}
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