1

Code below (js):

JSON = {"name":"John","stats":["canFly","invincible","noClip","canBuild"]};


function disableGodStats(JSON){ 

    for (i = 0; i < JSON.stats.length; i++) {
        if (JSON.stats[i] == "canFly" || JSON.stats[i] == "invincible" || JSON.stats[i] == "noClip" || JSON.stats[i] == "allowCommands") {
            JSON.stats.splice(i, 1);
            i--;
        }
    }

    return JSON;
}


disableGodStats(JSON);

The code snippet above I used for a small player database for a little online game.

when ran in Script Editor (for testing) it produces an output without quotation marks, which is weird, because the JSON code now does not validate for strict JSON which requires those.

Edit: Example output - notice missing quotes:

{name:John, stats:[canBuild]}

leanne
  • 7,940
  • 48
  • 77
  • What is this script editor? – Sterling Archer Aug 15 '16 at 13:13
  • 2
    Well, `JSON` contains a JavaScript object, not JSON. If you want to generate JSON, you need to use `JSON.stringify`. – Felix Kling Aug 15 '16 at 13:18
  • What comes out without quotations? the content of the array `stats` and `name`? what's the output? – DIEGO CARRASCAL Aug 15 '16 at 13:19
  • Old question, I know; and also *not* duplicate of suggested post. ***"Script Editor"*** is a Mac utility. It is a basic IDE for writing code in AppleScript and JavaScript. The "similar question" is about JQuery, which would *not* be applicable here. (Came across this looking for related information.) – leanne Nov 16 '19 at 18:48

1 Answers1

1

You are overwriting an Object called JSON, try using a different variable name.

Edit: This is the correct answer. Changing the variable name fixes the results. New code:

var JSON_item = {"name":"John","stats":["canFly","invincible","noClip","canBuild"]};


function disableGodStats(JSON){ 

    for (i = 0; i < JSON_item.stats.length; i++) {
        if (JSON_item.stats[i] == "canFly" || JSON_item.stats[i] == "invincible" || JSON_item.stats[i] == "noClip" || JSON_item.stats[i] == "allowCommands") {
            JSON_item.stats.splice(i, 1);
            i--;
        }
    }

    return JSON_item;
}


disableGodStats(JSON_item);

result, with appropriate quoting:

{"name":"John", "stats":["canBuild"]}

leanne
  • 7,940
  • 48
  • 77
AgentM
  • 406
  • 4
  • 18