1
export function getNoteKeys() {
    const notesObject = {}
    for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i)
        const value = localStorage[key]
        notesObject[key] = value
    }
    store.dispatch(SetActionForNotes(_.values(notesObject).sort(function(a, b) {
        return a > b
    })))
}

The current output I am getting for the above code is this...

{0: "a", 1: "b", 2: "c", 3: "d"}

The problem with this is that when I convert this to an array I do not have the ability to continue to uniquely identify each object after an item is removed, because the indexes of the items change if one is removed.

I want to set each key set to an id for every value retrieved. I have unique keys already set in localstorage, and I need them to uniquely identify each note in a list. I have tried many different ways and cannot figure this out.

As an example of what I am trying to do...

const obj = [{id: 1, value: "a"}, {id: 2, value: "b"}...]
7
  • Are you trying to use dynamic object keys, or just trying to map each key-value pair from localStorage to a new object shape? Your example output obj doesn't match what the code is doing and isn't a valid JS object. It's unclear what you are asking for. Also, the sort function comparator should return a number value, i.e. [-1, 0, 1], not a boolean to indicate sorting order. Commented Aug 3, 2021 at 6:29
  • You are already doing it with: notesObject[key] = value Commented Aug 3, 2021 at 6:30
  • {[{id: 1}, {value: "a"}], [{id: 2}, {value: "b"}]...} isn't valid object syntax, are you trying to get something like [{ id: 1, value: 'a' }, { id: 2, value: 'b' }, ...] in an array? Commented Aug 3, 2021 at 6:45
  • @DrewReese yes that is what I am aiming for. Ill edit my question so it will be correct. Commented Aug 3, 2021 at 6:47
  • Can you include also an example localStorage value so we can see where you're starting from? Commented Aug 3, 2021 at 6:48

2 Answers 2

1

Not sure what exactly is you purpose, but this will do the transformation you want.

Object.keys(localStorage).reduce((acc,key)=>{
  return[...acc,{id:key,value:localStorage[key]}]
},[])
Sign up to request clarification or add additional context in comments.

1 Comment

I am assuming value:asd[key] was added in error, your answer worked once I removed asd
1

It seems that localStorage is an object (versus array), and if so you can also use Object.entries and map the array of key-value pairs into [{ id: 1, value: 'a' }, { id: 2, value: 'b' }, ...].

Object.entries(localStorage).map(([id, value]) => ({ id, value }))

const localStorage = {
  0: 'a',
  1: 'b',
  2: 'c',
};

const res = Object.entries(localStorage).map(([id, value]) => ({ id, value }));

console.log(res);

Though since objects are just associative arrays of key-value pairs, it works with arrays as well.

const localStorage = ['a', 'b', 'c'];

const res = Object.entries(localStorage).map(([id, value]) => ({ id, value }));

console.log(res);

Comments

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.