I'm relatively new to web development, so this is probably a simple misunderstanding. I'm using an AJAX request to gather a list of used values within a specific range and then comparing it to a generated list of all possible values within that range to find the unused values.
I've tried using Lodash's _.difference() and the JavaScript Array filter() method, but they return the same results -- all values within the generated range -- which is leading me to believe it's related to the differing structures of the 2 arrays. These methods work when I create a test array like var used = ["0000000030000", "0000000030001"].
This is what the array's look like in the console: https://i.stack.imgur.com/ErwFC.jpg
The first array shows as [] until expanded; whereas, the other one shows the length of the array and values within?
// Will be set dynamically by subdepartment choice, used for testing at the moment.
var startRange = 30000;
var endRange = 39999;
// Pads the startRange and endRange to 13 digits with 0's for the AJAX request.
var startUPC = pad(startRange, 13);
var endUPC = pad(endRange, 13);
// AJAX request function to get all used UPC's within in range of numbers.
function GetNewPLU(callback) {
$.ajax({
url: "api/GetNewPLU",
type: "GET",
dataType: "json",
data: { 'startUPC': startUPC, 'endUPC': endUPC },
success: callback,
error: function (error) {
console.log(`Error ${error}`)
}
});
}
function SetNewPLU() {
// Executes GetNewPLU AJAX request function
var used = [];
GetNewPLU(function (data) {
for ($i = 0; $i < data.data.length; $i++) {
used.push( data.data[$i].OBJ_TAB.F01 );
}
});
var range = [];
// Generates array of all numbers between range.
for ($i = startRange; $i < endRange; $i++) {
range.push( '00000000' + $i );
};
//var unused = used.filter(function (n) { return !this.has(n), new Set(data) });
var unused = _.difference(range.toString(), used);
console.log(used);
console.log(range);
console.log(unused);
}
SetNewPLU();