2

I created an empty array in localStorage

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

and then fetching that empty array like this :

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

After fetching the Array I want to append some objects in the same.

Array of Objects

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',
  },
];

The first and last object have same id

Right now what I am doing is, as I don't want to save or append duplicate objects (by id key) in 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)
    }
  });
});

The above code is not working. I have also tried reduce method.

Note initially array is empty

12
  • 1
    localStorage.setItem does not automagically append arrays. It just writes the 2nd argument to the provided key (and in your current code, it's a single object, not the entire array.). Also it takes a string as second argument, you probably want to to move your stringify. Commented Mar 13, 2022 at 19:48
  • 2
    Why are you stringifying the result of setItem and not using the value? Commented Mar 13, 2022 at 19:48
  • @Lars Sorry!! I messed up!! I modified the code take a look Commented Mar 13, 2022 at 19:51
  • @CristianTraìna Check the code now 🙂 Commented Mar 13, 2022 at 19:55
  • 1
    Are you trying to continuously add items to the array? replace the items? Or do you want to keep the array to length = 1. I'm a little confused as to why you are setting the array in localstorage to an empty array each time. Also I don't see how you plan to access or append the array without parsing it back into a JavaScript object. Commented Mar 13, 2022 at 22:51

1 Answer 1

0

Let us take an example and try understanding , how you can do it with your code :

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");

Sign up to request clarification or add additional context in comments.

1 Comment

I mentioned initially the array is empty and I can also do this without using localStorage. Btw thanks for the effort. 🙂

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.