Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
cómo guardar una matriz en localstorage

Actualmente tengo este código, se guarda en el almacenamiento local, pero genera varios objetos, me gustaría almacenar todo en una sola matriz

 getItemById(id) { return this.cacheS.getOrSetCache(`StoreService_getItemById_${id}_${this.layout.emp.id}`, this.http.get(`${environment.API_URL}item/getById/${id}`), 300000); } getOrSetCache(key: string, request: Observable<any>, msToExpire = 3600000): Observable<any> { let cache: any = {}; cache = JSON.parse(localStorage.getItem(key)); return (cache?.data && (cache?.exp > Date.now())) ? of(cache.data) : request.pipe(tap(v => { localStorage.setItem(key, JSON.stringify({data: v, exp: (Date.now() + msToExpire)})); })); }

ingrese la descripción de la imagen aquí

Este código funciona, pero quería unir todos los StoreService_getItemById_ , con este código hace que la matriz sea una sola StoreService_getItemById_ , si nota que la imagen tiene varios almacenamientos locales con el nombre StoreService_getItemById_, me gustaría que tuviera solo uno y dentro de eso tenía los objetos en formato de matriz

 localStorage.setItem(key, JSON.stringify({data: [v], exp: (Date.now() + msToExpire)}));

Ojalá fuera el mismo "artículo" ingrese la descripción de la imagen aquí

ingrese la descripción de la imagen aquí

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Parece que desea agregar elementos a una matriz en el almacenamiento local. La cuestión es que el almacenamiento local solo guarda cadenas . Puede analizar la cadena para convertirla en una matriz, agregar un nuevo elemento y luego sobrescribir la cadena anterior.

 const key = 'my-array-key'; const someData = { name: 'name', id: 'abcdef' }; const msToExpire = 5000; // Convert string to array, initializes empty array if string is empty let arr: any[] = []; let string = localStorage.getItem(key); if (string) arr = JSON.parse(string); // Push a new item and overwrite the old string arr.push({data: someData, exp: (Date.now() + msToExpire)}); localStorage.setItem(key, JSON.stringify(arr));

No estoy muy seguro de cómo quieres estructurar esa función, pero este ejemplo debería darte una idea general.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!