Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently I have a working function that individually goes through, obtains the set synced data in Chrome and then sets the value of a corresponding ID element with the returned value of the get process.

function getSettings() {
    chrome.storage.sync.get("streamSelect", function (result) {
        var element = document.getElementById('streamSelect');
        element.value = result.streamSelect;
    });

    chrome.storage.sync.get("volumeSlider", function (result) {
        var element = document.getElementById('volumeSlider');
        element.value = result.volumeSlider;
    });

    chrome.storage.sync.get("volumeBar", function (result) {
        var element = document.getElementById('volumeBar');
        element.value = result.volumeBar;
    });

    chrome.storage.sync.get("currentSegment", function (result) {
        var element = document.getElementById('currentSegment');
        element.value = result.currentSegment;
    });

    chrome.storage.sync.get("nowPlaying", function (result) {
        var element = document.getElementById('nowPlaying');
        element.value = result.nowPlaying;
    });

    chrome.storage.sync.get("lastPlayed", function (result) {
        var element = document.getElementById('lastPlayed');
        element.value = result.lastPlayed;
    });

    chrome.storage.sync.get("quickLinks", function (result) {
        var element = document.getElementById('quickLinks');
        element.value = result.quickLinks;
    });

    chrome.storage.sync.get("desktopNotifications", function (result) {
        var element = document.getElementById('desktopNotifications');
        element.value = result.desktopNotifications;
    });
}

I have then tried to convert this into an array to perform the same function but in a much smaller footprint, however I am running into issues and this is not returning the same results as the expanded code presented above. As it executes up to the chrome.storage.sync.get line, but then fails from there after.

function getSettings2() {
    var items = ["streamSelect", "volumeSlider", "volumeBar", "currentSegment", "nowPlaying", "lastPlayed", "quickLinks", "desktopNotifications"];

    for(var i = 0, length = items.length; i < length; i++) {
        selectedItem = items[i];
        chrome.storage.sync.get(selectedItem, function (result) {
            var element = document.getElementById(selectedItem);
            element.value = result.selectedItem;
        });
    };
}

Any help to find a fix to this would be much appreciated, as I have a range of places which have expanded code that I wish to condense into smaller array functions.

share|improve this question

put on hold as off-topic by Mike Brant, t3chb0t, EBrown, forsvarir, Joseph the Dreamer 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Mike Brant, t3chb0t, EBrown, forsvarir, Joseph the Dreamer
If this question can be reworded to fit the rules in the help center, please edit the question.

    
You should post the question on StackOverflow, as that is a better forum to deal with questions on broken code. Here we only deal with reviewing code that is working. Your idea to simplify this code is the right one. – Mike Brant 2 days ago
    
While the second code snippet is broken, the first is not. The first code is reviewable here. – mdfst13 2 days ago
    
@mdfst13 You are correct. The reviews would consist of: "Try to do what you are doing in the second snippet", "Make it DRY", etc. :) – Mike Brant 2 days ago
    
I attempted to step past the expected "Make it DRY" and to showcase that I've attempted that with no avail. Should I remove the second code and leave the first code to be reviewed or move the whole post over to StackOverflow? – BlairSille 2 days ago