Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9263715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:39:08+00:00 2026-06-18T13:39:08+00:00

I have following code var s = storage(‘foo’); s.bar = 100; storage(‘foo’, s); storage

  • 0

I have following code

var s = storage('foo');
s.bar = 100;
storage('foo', s);

storage returns an object from some storage media. I then change the value of one property of the object and send it to storage again. My question is is there any way to make it a one liner? Something like

storage('foo', storage('foo').bar = 100);

But the above code only saves 100 instead of entire foo object.

Here is the storage function, it stores on localstorage but makes it object storage instead of string storage:

function storage(item, value) {
    if(value!=null) {
        var result = (storage()) ? storage() : {};
        if(item) result[item] = value;
        return localStorage.setItem('storage', JSON.stringify(result))
    } else return (item) ? storage()[item] : JSON.parse(localStorage.getItem('storage'));
}

Edit:
So I ended up with following one line change in storage function. But this does not account for multidimensional usage. So I will welcome any suggestions.

function storage(item, value, property) {
    ...
        if(item) (property) ? result[item][property] = value : result[item] = value;
    ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T13:39:09+00:00Added an answer on June 18, 2026 at 1:39 pm

    The reasonably thing to do, if you cannot change the storage function, is to create another function:

    function set_storage(obj, prop, val) {
        var s = storage(obj);
        obj[prop] = val;
        storage(obj, s);
    }
    
    // usage
    set_storage('foo', 'bar', 100);
    

    If you can change storage though, this would be an even better way. jQuery.data works similarly.

    There are ways to do this in (almost) one line, with the help of the comma operator [MDN], but it ain’t pretty:

    var s = storage('foo');
    storage('foo', (s.bar = 100, s));
    

    Since you said storage is your function, overloading it seems to be a good way. It would go like this:

    function storage(key, prop, val) {
        var storage = JSON.parse(localStorage.getItem('storage'));
    
        if(arguments.length === 0) {
            return storage;
        }
        else if (arguments.length === 1) {
            // only one argument passed, return the value with key `key`
            return storage[key];
        }
        else if (arguments.length === 2) {
            // set `key` to `prop` (`prop` contains the actual value)
            storage[key] = prop;
        }
        else {
            // set property `prop` of object in `key` to value `val`
            storage[key][prop] = val;
        }
        localStorage.setItem('storage', JSON.stringify(storage));
    }
    
    // Usage
    
    // get `foo`:
    var foo = storage('foo');
    
    // set `foo`:
    storage('foo', {bar: 42});
    
    // set foo.bar
    storage('foo', 'bar', 100);
    

    I hope it gives you some idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var from = 0, step = 5; function showNext(list)
I have the following code: var thing = (from t in things where t.Type
I have the following code: var currentUser = (from i in _dbContext.Users where i.FirstName
I have the following code: var submitHandler = function ($link, $form, close) { var
I have the following code: var refreshId = setInterval(function() { $(#footad).html('myjshere'); }, 15500); Where
I have the following code: var carsContext = new CarsDataContext(); IQueryable<Car> allCars = carsContext.Cars;
I have the following code var dbvalue = Ned; var deleteLink = '<a href=#delete
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
I have the following code: var bool:String = true; Without an if block or
I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.