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

252
Views
Cómo encontrar si existe un objeto en una matriz [la matriz proviene del almacenamiento local]

Creé una matriz vacía en localStorage

 localStorage.setItem('items', JSON.stringify([]))

y luego obtener esa matriz vacía como esta:

 let fetchedItems = JSON.parse(localStorage.getItem('items'));

Después de obtener el Array , quiero agregar algunos objetos en el mismo.

Matriz de objetos

 let objects = [ { id: '37f60b13-bb3a-4919-beff-239207745343', body: '1', }, { id: '26c5b0fa-b15f-4a50-9a56-5880727a8020', body: '2', }, { id: '37f60b13-bb3a-4919-beff-239207745343', body: '1', }, ];

El primer y último objeto tienen la misma id

En este momento, lo que estoy haciendo es, ya que no quiero guardar o agregar objetos duplicados (por clave de identificación) en array/localstorage :

 function saveItemsInLocalStorage(item) { let items; if (localStorage.getItem('items') === null) { items = []; } else { items = JSON.parse(localStorage.getItem('items')); } items.push({ id: item.id, body: item.body, }); localStorage.setItem('items', JSON.stringify(items)); } objects.forEach((object) => { fetchedItems.forEach((fetchedItem) => { if (fetchedItem.id !== object.id) { saveItemsInLocalStorage(object) } }); });

El código anterior no funciona. También probé el método de reduce .

Tenga en cuenta que inicialmente la matriz está vacía

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tomemos un ejemplo e intentemos comprender cómo puede hacerlo con su código:

 let obj = {name:"user",id:1}; let arr = [{name:"user",id:2},{name:"user",id:3}]; let present = false ; arr.map(val=>{ if(JSON.stringify( {...val})===JSON.stringify({...obj}) ) present = true ; }) if(present)console.log("The object is present") else console.log("The object is not present");

about 4 years ago · Juan Pablo Isaza 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!