0

I have trawled the net and stackoverflow for an answer to this question.

How does one retrieve a value from a dynamic key which is also nested in localstorage. I have tried numerous examples and seem to be getting nowhere. I knwo a loop is required to perform the task but which type.

How do I get the 'uniqid' value which has the nested dynamic key SCI-4 from localstorage which reads:

simpleCart_items:{"SCI-4":{"quantity":4,"id":"SCI-4","price":8,"name":"12 fried chicken","flavours":"cola","code":"1001","cartid":"561","uniqid":"592dcd08b5fcc"}}

This allows me to view localstorage.

var obj = localStorage;                   
Object.keys(obj).forEach(function(key){
    var value = obj[key];
    console.log(key + ':' + value);
});

How do I iterate to get any value?

The difference in this question is that I seek to get the value from a nested key that has a dynamic value. If the key is not known how does one pull the value? What should the loop look like?

  • 1
    Possible duplicate of [Looping through localStorage in HTML5 and JavaScript](https://stackoverflow.com/questions/3138564/looping-through-localstorage-in-html5-and-javascript) – Will P. May 30 '17 at 20:53
  • `obj[key].uniqid` (this question isn't so much about `localStorage` as it is about accessing parts of an object tree) –  May 30 '17 at 20:57

3 Answers3

1

After a good nights sleep. I am able to make headway. Thank you for pointing me in the right direction guys. There are many opinions and ways to do some of these functions which got confusing.

localStorage needed to be accessed properly as I show below. The issue is mostly resolved with:

var obj = JSON.parse(localStorage.simpleCart_items);

                        Object.keys(obj).forEach(function (key) {
                            Object.keys(obj[key]).forEach(function (val) {
                                //console.log(val);
                                if (val === "quantity")
                                {
                                    //logic here
                                    console.log(obj[key][val]);
                                }
                            });
                        });
  • Glad you are making progress. You should up vote all the answers that helped you and mark one as "the" answer if it was the one that made it work for you. – Scott Marcus May 31 '17 at 14:34
0

think this is what your're looking for, :)

Object.keys(obj).forEach(function (key) {
Object.keys(obj[key]).forEach(function (val) {
  //console.log(val);
  if(val === "uniqid")
  {
   //logic here
   console.log(obj[key][val]);
  }
});

});

  • `Object.keys(obj).forEach(function (key) { Object.keys(obj[key]).forEach(function (val) { console.log(val); }); });` – Evil Dr. PorkChop May 30 '17 at 21:03
  • `Object.keys(obj).forEach(function (key) { Object.keys(obj[key]).forEach(function (val) { //console.log(val); if(val === "uniqid") { //logic here console.log(obj[key][val]); } }); });` – Evil Dr. PorkChop May 30 '17 at 21:05
  • Thank you for your reply. I see the method your answer provides, however the console.log is empty. This seems to be the closest to correct. I will fiddle to see if I can employ the solution. – rbwilkinson May 30 '17 at 21:41
  • of interesting it worked when i had the json object hmm, but seems you got a hang for it :) Gl!~ – Evil Dr. PorkChop May 30 '17 at 22:23
  • Odd. I uncommented 'console.log(val);' and i get a numbers list up to 144. What value does that represent? – rbwilkinson May 30 '17 at 22:44
  • val is the property on on the object for example, on object "SCI-4": when the property === "uniqid" I'm simply saying to console the value of the SCI-4.uniqid which is "592dcd08b5fcc" "SCI-4": { "quantity":4, "id":"SCI-4", "price":8, "name":"12 fried chicken", "flavours":"cola", "code":"1001", "cartid":"561", "uniqid":"592dcd08b5fcc" } – Evil Dr. PorkChop May 30 '17 at 23:23
0

If you just store your structure as a single value for a single key, it's pretty easy to extract it later.

// How do I get the 'uniqid' value which has the nested dynamic key SCI-4 from localstorage which reads:

// This is our simulated result from pulling from localStorage
var lsString = `{ 
                  "simpleCart_items": { 
                      "SCI-4": {
                        "quantity":4,
                        "id":"SCI-4",
                        "price":8,
                        "name":"12 fried chicken",
                        "flavours":"cola",
                        "code":"1001",
                        "cartid":"561",
                        "uniqid":"592dcd08b5fcc"
                       }
                   }
}`;


// First, remember that localStorage stores strings, so we must parse the string 
// back into an object:
var lsObj = JSON.parse(lsString);

var keyName = Object.keys(lsObj.simpleCart_items).toString();

// Now, we can go directly to the property we want:
console.log(lsObj.simpleCart_items[keyName].uniqid);
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • The key SCI-4 is dynamic. There is a range of SCI-* that needs to be considered. – rbwilkinson May 30 '17 at 22:05
  • @rbwilkinson Answer updated. Now, the key name is dynamic. – Scott Marcus May 30 '17 at 22:17
  • Yes it is dynamic. As in the title of my question. I provide the key as an example. – rbwilkinson May 30 '17 at 23:00
  • @rbwilkinson I'm not asking *if* it's dynamic. I'm telling you that my answer has been updated so that it is now able to get that key dynamically. – Scott Marcus May 30 '17 at 23:11
  • I am sorry about that. I am getting an error concerning unexpected character when I grab from localStorage. It works when I test with directly from lsString array as you have laid it out. This needs to be able to loop as well. Is this acceptable: `var lsString = localStorage; var lsObj = JSON.parse(lsString); var keyName = Object.keys(lsObj.simpleCart_items).toString();` – rbwilkinson May 30 '17 at 23:51
  • @rbwilkinson You have a mistake with: `var lsObj = JSON.parse(lsString)`. In my example, I'm not actually going to `localStorage` and getting the string - I've hard coded the sample string. You will have to go and get it out, so the line would be `var lsObj = JSON.parse(lsString.getItem("Your Local Storage Key Name Here"))` – Scott Marcus May 30 '17 at 23:58