简体   繁体   English

AJAX JSON响应返回正确的值,但两者之间是否还有未定义的值?

[英]AJAX JSON response is returning the correct values, but also undefined values in between?

I'm working on some JavaScript code that is going to find the smallest unused number in a specified range of numbers. 我正在研究一些JavaScript代码,这些代码将在指定的数字范围内找到最小的未使用数字。 The AJAX request gathers all used numbers in the specified range, but is additionally returning undefined values in between the correct values? AJAX请求会收集指定范围内的所有已用数字,但是是否还会在正确值之间返回未定义的值? I've compared the returned values from the JSON response to what's currently in the SQL table and all the correct values are in both the JSON response and the SQL table, so I'm not sure where the undefined values are coming from. 我已经将JSON响应的返回值与SQL表中的当前值进行了比较,并且所有正确的值都在JSON响应和SQL表中,因此我不确定未定义的值来自何处。

https://imgur.com/a/rXLfEJk https://imgur.com/a/rXLfEJk

JavaScript: JavaScript:

//Specified range of numbers to search.
var startRange = 40000;
var endRange = 49999;

//UPC's are padded with to 13 digits with 0's then sent as parameters.
var startUPC = '00000000' + startRange;
var endUPC = '00000000' + endRange;

// AJAX call to web API that gathers all UPC's within a range.
$.ajax({
    url: "api/GetNewPLU",
    type: "GET",
    dataType: "json",
    data: { 'startUPC': startUPC, 'endUPC': endUPC },
    success: function (data) {
        $.each(data.data, function (i, UPCs) {
            for (var i in UPCs) {
                console.log("UPC: " + UPCs[i].F01);
            }
        })
    },
    error: function (error) {
        console.log(`Error ${error}`)
    }
})

JSON Response: JSON响应:

{
    "draw": null,
    "data": [{
        "DT_RowId": "row_0000000040002",
        "OBJ_TAB": {
            "F01": "0000000040002"
        }
    }, {
        "DT_RowId": "row_0000000040008",
        "OBJ_TAB": {
            "F01": "0000000040008"
        }
    }, {
        "DT_RowId": "row_0000000040013",
        "OBJ_TAB": {
            "F01": "0000000040013"
        }
    }, {
        "DT_RowId": "row_0000000040017",
        "OBJ_TAB": {
            "F01": "0000000040017"
        }
    }
}

I plan to loop through the used numbers from the AJAX request comparing them to a sequentially incremented generated number until there is not a match and then save that unused number. 我计划遍历AJAX请求中使用的数字,并将它们与按顺序递增的生成数字进行比较,直到没有匹配为止,然后保存该未使用的数字。 I'm not sure if it's worth figuring out why I'm returning both values and undefined or if I should just find a way to filter out the undefined. 我不确定是否值得弄清楚为什么我同时返回值和未定义值,或者是否应该找到一种方法来过滤未定义值。

lodash _.difference can help to you. lodash _.difference可以为您提供帮助。
Create two arrays. 创建两个数组。 One for generated values, one for used values. 一种用于生成的值,一种用于使用的值。 And find diff between them. 并找到它们之间的差异。

var created = ["0000000040000", "0000000040001", "0000000040002"];
var used = ["0000000040001", "0000000040002"];
var unused = _.difference(created, used);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM