0

When I print array on Chrome console, It is added unexpected value end of array. Here is my screenshot. When I execute step by step using breakpoint in Chrome console, it works correct.

enter image description here

But when I execute not using breakpoint, new values were added at the end of each array.

enter image description here

I don't know that's why. Please help me.

Here is code.

var sample2 = {
    "menus": [
        {
            "command": "formatblock",
            "header": "- formatting -",
            "values": {
                "h1": "Title 1 <h1>",
                "h2": "Title 2 <h2>",
                "h3": "Title 3 <h3>",
                "h4": "Title 4 <h4>",
                "h5": "Title 5 <h5>",
                "h6": "Title 6 <h6>",
                "p": "Paragraph <p>",
                "pre": "Preformatted <pre>"
            }
        },
        {
            "command": "fontname",
            "header": "- font -",
            "values": [
                "Arial",
                "Arial Black",
                "Courier New",
                "Times New Roman"
            ]
        },
        {
            "command": "fontsize",
            "header": "- size -",
            "values": {
                "1": "Very small",
                "2": "A bit small",
                "3": "Normal",
                "4": "Medium-large",
                "5": "Big",
                "6": "Very big",
                "7": "Maximum"
            }
        },
        {
            "command": "forecolor",
            "header": "- color -",
            "values": {
                "red": "Red",
                "blue": "Blue",
                "green": "Green",
                "white": "White",
                "black": "Black"
            }
        },
        {
            "command": "backcolor",
            "header": "- background -",
            "values": {
                "white": "White",
                "red": "Red",
                "green": "Green",
                "black": "Black"
            }
        }
    ],
    "buttons": [
        {
            "text": "Clean",
            "command": "cleanDoc",
            "image": "icons\/clean.gif"
        },
        {
            "text": "Print",
            "command": "printDoc",
            "image": "icons\/print.png"
        },
        {
            "text": "Undo",
            "command": "undo",
            "image": "icons\/undo.gif"
        },
        {
            "text": "Redo",
            "command": "redo",
            "image": "icons\/redo.gif"
        },
        {
            "text": "Remove formatting",
            "command": "removeFormat",
            "image": "icons\/format.png"
        },
        {
            "text": "Bold",
            "command": "bold",
            "image": "icons\/bold.gif"
        },
        {
            "text": "Italic",
            "command": "italic",
            "image": "icons\/italic.gif"
        },
        {
            "text": "Underline",
            "command": "underline",
            "image": "icons\/underline.gif"
        },
        {
            "text": "Left align",
            "command": "justifyleft",
            "image": "icons\/justifyleft.gif"
        },
        {
            "text": "Center align",
            "command": "justifycenter",
            "image": "icons\/justifycenter.gif"
        },
        {
            "text": "Right align",
            "command": "justifyright",
            "image": "icons\/justifyright.gif"
        },
        {
            "text": "Numbered list",
            "command": "insertorderedlist",
            "image": "icons\/numberedlist.gif"
        },
        {
            "text": "Dotted list",
            "command": "insertunorderedlist",
            "image": "icons\/dottedlist.gif"
        },
        {
            "text": "Quote",
            "command": "formatblock",
            "value": "blockquote",
            "image": "icons\/quote.gif"
        },
        {
            "text": "Delete indentation",
            "command": "outdent",
            "image": "icons\/outdent.gif"
        },
        {
            "text": "Add indentation",
            "command": "indent",
            "image": "icons\/indent.gif"
        },
        {
            "text": "Hyperlink",
            "command": "createLink",
            "image": "icons\/hyperlink.gif"
        },
        {
            "text": "Cut",
            "command": "cut",
            "image": "icons\/cut.gif"
        },
        {
            "text": "Copy",
            "command": "copy",
            "image": "icons\/copy.gif"
        },
        {
            "text": "Paste",
            "command": "paste",
            "image": "icons\/paste.gif"
        }
    ]
}

function createRow(input, output, parent, id, d, key) {
    id = output.length;
    var newRow = [id, d, parent, key, getEntityType(input)];
    if(getEntityType(input) === "String") newRow.push(input);
    return newRow;
}

function obj2Array2(input, output, parentID, id, d, self) {
    if (output == undefined) { var output = []; }
    if (!d) { var d = 0; }
    d = d + 1;
    if (parentID == undefined) {
        var parentID = "root";
    };
    newRow = createRow(input, output, parentID, id, d, self)
    output.push(newRow);

    if (getEntityType(input) === 'Object') {
        for (var key in input) {
            if (!input.hasOwnProperty(key)) continue;
            obj2Array2(input[key], output, self, id, d, key);
        }
    } else if (getEntityType(input) === "Array") {
        for (var i = 0; i < input.length; i++) {
            obj2Array2(input[i], output, self, id, d, i);
        }
    }
    return output;
}

var outputArray = obj2Array2(sample2, []);
console.log(outputArray)
SlobodanDev
  • 35
  • 1
  • 4
  • 1
    Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) Post CODE. not PICTURES of code – mplungjan Dec 10 '20 at 18:33
  • Side note, you need to take a moment to review https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript `if (!d) { var d = 0; }` <= the var is pointless there. You already have a variable defined on the argument list. It just doesn't have a truthy value. – Taplar Dec 10 '20 at 18:34
  • Thanks mplungjan. I have added example code. Please check and if you have answer, let me know. – SlobodanDev Dec 11 '20 at 06:32

0 Answers0