只有在JSON数据的最后一部分时才能通过函数

时间:2022-12-12 17:00:07

I'm trying to run a function on every member of JSON data. It only runs it on the last part of that though. The code is messy because I've been adding and removing debugging for hours. I'm a noob and JS :(

我正在尝试在JSON数据的每个成员上运行一个函数。它只在它的最后部分运行它。代码很乱,因为我一直在添加和删除调试几个小时。我是菜鸟和JS :(

This is the checkIt function code: (the checkBlacklist.php returns either a true or false)

这是checkIt函数代码:( checkBlacklist.php返回true或false)

 var nope = "0";
function checkIt(id, _callback) {
$.post("checkBlacklist.php?id=" + id, function(data) {
          console.log("posted for " + id + " with " + data);
     if (data == "false") {
        nope = 1;
        _callback();
     } else { nope = 2; _callback(); }
  });
 }

And here is the code that goes through the JSON at http://robloxplus.com:2052/inventory?username=iTracking

以下是http://robloxplus.com:2052/inventory?username=iTracking中通过JSON的代码

$.get("http://robloxplus.com:2052/inventory?username=iTracking").success(function(r){
for(var id in r.data.hat.data){
    $( "h2" ).empty(); // remove the "Items Loading" title
if (r.data.hat.data[id].rap > 1000 && r.data.hat.data[id].rap < 5000) { 
// HERE IS WHERE IT MESSES UP, IT'S REALLY WEIRD. 
console.log(r.data.hat.data[id].name);
var selectedNope = 0; 
checkIt(r.data.hat.data[id].id, function() {
    selectedNope = nope;
    nope = 0;
    console.log(selectedNope + " for " + r.data.hat.data[id].name);
        if (selectedNope == 1) {
            $("body").append("<center><div class='itemContainer_blue'><div class='itemName'><a href='http://www.roblox.com/---item?id=" + r.data.hat.data[id].id + "'>" +   r.data.hat.data[id].name + "</a></div><div class='itemRap'>RAP: " + r.data.hat.data[id].rap + "</div><div class='itemImage'> <a href='#' title='Item Name'> <center><img src='" + r.data.hat.data[id].image + "' alt='itemImage'/></center></a></div> <div class='itemPrice'>Manual Price Set</div></div></center>");
        } else { console.log("it was not 1, it was " + selectedNope + " for " +  r.data.hat.data[id].name); }
});

} 
});

Okay, so what it does is:

好的,它的作用是:

Output:

Outputs all the hat names on the console.log before checkIt

在checkIt之前输出console.log上的所有帽子名称

Once inside the checkIt, it outputs only one ID, the last ID in the JSON list called Steampunk Tricorn TWICE, which takes the replacement of the two hats that are actually supposed to be there.

一旦进入checkIt,它只输出一个ID,即JSON列表中最后一个名为Steampunk Tricorn TWICE的ID,它取代了实际应该存在的两个帽子。

Appends the Steampunk Tricorn only.

仅附加Steampunk Tricorn。

The Steampunk Tricorn shouldn't even get inside of the if statement that says

Steampunk Tricorn甚至不应该进入if声明中

if (r.data.hat.data[id].rap > 1000 && r.data.hat.data[id].rap < 5000) { 

Basically it switches everything that is supposed to be there with something that is not (Steampunk Tricorn)

基本上它会切换那些应该存在的东西(Steampunk Tricorn)

Sorry if that was SUPER confusing, I tried my best to explain it.

很抱歉,如果那太令人困惑,我会尽力解释。

1 个解决方案

#1


I think your problem is that the variable is not behaving as you are expecting. (Also see: https://*.com/a/2853627/1938640)

我认为你的问题是变量不像你期望的那样表现。 (另见:https://*.com/a/2853627/1938640)

This fix might work:

此修复可能有效:

function checkHat(hat) {
    if (hat.rap > 1000 && hat.rap < 5000) {
        $.post("checkBlacklist.php?id=" + hat.id, function (data) {
            console.log(data + " for " + hat.name);
            if (data == "false") {
                $("body").append("<center><div class='itemContainer_blue'><div class='itemName'><a href='http://www.roblox.com/---item?id=" + hat.id + "'>" + hat.name + "</a></div><div class='itemRap'>RAP: " + hat.rap + "</div><div class='itemImage'> <a href='#' title='Item Name'> <center><img src='" + hat.image + "' alt='itemImage'/></center></a></div> <div class='itemPrice'>Manual Price Set</div></div></center>");
            } else {
                console.log("it was not 1, it was " + data + " for " + hat.name);
            }
        });
    }
}

$.get("http://robloxplus.com:2052/inventory?username=iTracking").success(function (r) {
    $("h2").empty();
    for (var id in r.data.hat.data) {
        checkHat(r.data.hat.data[id]);
    }
});

#1


I think your problem is that the variable is not behaving as you are expecting. (Also see: https://*.com/a/2853627/1938640)

我认为你的问题是变量不像你期望的那样表现。 (另见:https://*.com/a/2853627/1938640)

This fix might work:

此修复可能有效:

function checkHat(hat) {
    if (hat.rap > 1000 && hat.rap < 5000) {
        $.post("checkBlacklist.php?id=" + hat.id, function (data) {
            console.log(data + " for " + hat.name);
            if (data == "false") {
                $("body").append("<center><div class='itemContainer_blue'><div class='itemName'><a href='http://www.roblox.com/---item?id=" + hat.id + "'>" + hat.name + "</a></div><div class='itemRap'>RAP: " + hat.rap + "</div><div class='itemImage'> <a href='#' title='Item Name'> <center><img src='" + hat.image + "' alt='itemImage'/></center></a></div> <div class='itemPrice'>Manual Price Set</div></div></center>");
            } else {
                console.log("it was not 1, it was " + data + " for " + hat.name);
            }
        });
    }
}

$.get("http://robloxplus.com:2052/inventory?username=iTracking").success(function (r) {
    $("h2").empty();
    for (var id in r.data.hat.data) {
        checkHat(r.data.hat.data[id]);
    }
});