Tengo una matriz de objetos que se parece a esto:
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' }, ], }, ....... ] Ahora quiero acceder al elemento de la matriz donde ticketId es igual a aaa y cambiar su propiedad de threads .
He intentado hacerlo usando tickets['aaa'].threads = [ ... ] pero arroja estos errores:
Eslint: Unsafe member access .threads on an 'any' value
TypeScript: Element implicitly has an 'any' type because index expression is not of type 'number'
Encuentra el índice donde ticketId es igual a "aaa"
tickets[tickets.findIndex(v => v.ticketId === "aaa")].threads = [/* ... */];Con validación de índice:
const idx = tickets.findIndex(v => v.ticketId === "aaa"); if (idx > -1) tickets[idx].threads = [/* ... */];Si simplemente desea mutar por propiedad, puede obtener el índice con indexOf y mutar
tickets[tickets.findIndex(ticket => ticket.ticketId === "aaa")].threads = [] //mutate here however you wantEspero que lo siguiente sea útil para usted.
var getItem = id => tickets.find(item => item.ticketId === id); getItem("aaa"); // {ticketId: 'aaa', authorId: 'abc', replyCount: 0, status: 'Open', threads: Array(1)}