I have an array of objects stored in local storage, but I want to be able to use the contents of each of the object in different parts of the website

88 views Asked by At

array of objects

i am expecting to be able to target the id and use it where I want and the name and also use it where i want.

3

There are 3 answers

0
Ahmed NourEl-Din Mohamed Awad On

You can use Redux & Redux Toolkit in ReactJS and make your store with reducers and actions then using useSelector Hook to use it in any component.

Tutorials for this: https://redux-toolkit.js.org/tutorials/quick-start

0
user20386762 On

After you get the array of objects from the localStorage, you can use .find() to find a single object by id, for example. You could also use .filter() if you need to filter array down based on some other property.

0
Oleg Brazhnichenko On

If you store an array of objects, you can't just take an object from local storage, you should take whole array and then take an object from it.

The other way, you can store those objects under different keys, then you will be able to just get one exact object from local storage.