0

This is part of a JSON data I'm trying to make an array of and put them in a Google Spreadsheet,

This is the m part

"m":{
"414" : {"a":123},
"47279":{"a":1234 },
"463306":{"a":12345}}

This is the z part

"z":{"3270":
"414" : {"d":{"0":{"p":500}}},
"47279":{"d":{"0":{"p":700}}}}

This is the JS code I'm Using

var Uid = Object.getOwnPropertyNames(doc1.m);
for (var lp2 = 0; lp2 < Uid.length; lp2++) {
    var Uid1 = Uid[lp2];
    var TotalD = Math.round(doc1.m[Uid[lp2]].a);




    testUF.push([Uid1, TotalD]);
}
//Total Obelisk Defense
for (var lp1 = 0; lp1 < testUF.length; lp1++) {
    var lp35id = Object.getOwnPropertyNames(doc1.z["3270"]).length;
    var ob35id = Object.getOwnPropertyNames(doc1.z["3270"]);
    var ob35 = "3270";

    for (var lp3 = 0; lp3 < lp35id; lp3++) {
        if (ob35id[lp3] === testUF[lp1][0]) {
            if (typeof doc1.z[ob35][ob35id[lp3]] !== 'undefined' && doc1.z[ob35][ob35id[lp3]] !== null && doc1.z[ob35][ob35id[lp3]] !== "") {
                var ob35D = (doc1.z[ob35][ob35id[lp3]].d["0"].p);
            } else {
                var ob35D = "0";
            }
        }

        var ob35TD = ob35D;
    }

    var obTD = ob35TD;
    testUF[lp1][2] = Math.round(obTD);

}

The Result I'm Getting

+--------+--------+------+
| Uid1   | TotalD | obTD |
+--------+--------+------+
| 414    | 123    | 500  |
+--------+--------+------+
| 42729  | 1234   | 700  |
+--------+--------+------+
| 463306 | 12345  | 700  |  <<-- This is the Problem, The obTD Value should be 0
+--------+--------+------+

So the problem is in my result you can see the obTD value just duplicates from the previous value when the Uid does not exist in the "m" object, but I want it to be 0 if the Uid doesn't exist in the "z":{"3270" object.

erajtob
  • 15
  • 5

1 Answers1

0

You've run into something you didn't expect with variable scoping. You intend to declare ob35D in the scope of your innermost for loop, leaving it undefined elsewhere, but JavaScript only has function-level scoping. Since you don't declare any functions in what you've posted, all of your variables are effectively global to this section of your code.

So, as you run through your lp1 and lp3 loops, (ob35id[lp3] === testUF[lp1][0]) is true for Uids 414 and 42729 and sets ob35D to 500 then 700. On the final lp1 loop, the lp3 loop never finds a match for 463306, so ob35D remains 700 and get propagated through ob35TD and obTD (which are actually unnecessary because ob35D is accessible everywhere) into your testUF list.

The simplest fix is to set ob35D = 0 at the beginning of each lp1 loop. That gives you the default you want when there's no Uid match.

I would recommend also reading up on JavaScript variable scope, since with a better understanding of it you can significantly simplify your code.

Community
  • 1
  • 1
Kristján
  • 18,165
  • 5
  • 50
  • 62
  • ThanQ very much man, It solved my problem, plus also gave me the idea of what and why I was getting that error. ThanQ very much (: – erajtob Oct 14 '15 at 16:29