0

I have that following function which updates a single property inside the localStorge

   /**
     * Update a single property 
     * @param key
     * @param value
     * @return Bool
     */
    SelectedCampaign.updateProperty = function (key, value) {
        var savedCampaign = localStorageService.get(selecteCampaignKey);
        savedCampaign[key] = value;
        success = localStorageService.set(selecteCampaignKey, savedCampaign);
        return success;
    };

So if i'm sending for example

updateProperty('name','The name')

, it will update the name key of the object.

I want to improve this function so it will be able to update a multidimensional key - For example

updateProperty('location.city.zone','The zone')

I'm having a hard time to create a function which does it. It probably needs some recursive function do it. Also, it needs to validate that the key is valid (not undefined/null) but an object Any idea?

Nonemoticoner
  • 650
  • 5
  • 14
Aviv Paz
  • 1,051
  • 3
  • 13
  • 28
  • Possible duplicate of [Convert JavaScript string in dot notation into an object reference](http://stackoverflow.com/questions/6393943/convert-javascript-string-in-dot-notation-into-an-object-reference) – Grundy Mar 09 '16 at 16:05
  • Local Storage is capable only text, not arrays nor objects nor null nor undefined values. The key should work `location.city.zone` since that's a text key. What's the problem? – Marcos Pérez Gude Mar 09 '16 at 16:06

0 Answers0