I am pulling in some data from database using Ajax. The data is stores as a integer within the database.
I am adding each row to an array to make up a binary string of what is coming in for later comparison.
The code is shown below. The issue I have is the result has an undefined at the front of each array.
$.ajax({
url: "######.php",
dataType: "json",
success: function (data) {
len = Object.keys(data).length;
for (i = 0; i <= len - 1; i++) {
markerCatBinary[i] += data[i]['motorhome']
markerCatBinary[i] += data[i]['caravan'];
markerCatBinary[i] += data[i]['tent'];
markerCatBinary[i] += data[i]['elect'];
markerCatBinary[i] += data[i]['toilet'];
markerCatBinary[i] += data[i]['shower'];
markerCatBinary[i] += data[i]['dog'];
markerCatBinary[i] += data[i]['fishing'];
markerCatBinary[i] += data[i]['beach'];
markerCatBinary[i] += data[i]['swimming'];
markerCatBinary[i] += data[i]['pub'];
markerCatBinary[i] += data[i]['adult'];
}
console.log("incoming Binary = ", markerCatBinary);
}
});
Console log result
incoming Binary =
Array(4)
0: "undefined111110100010"
1: "undefined100000110010"
2: "undefined100000110010"
3: "undefined100000101000"
length: 4
__proto__: Array(0)
I have tried parseInt but this gives Nan on all arrays
below example of first row data return from database
adult: "0"
beach: "0"
caravan: "1"
dog: "1"
elect: "1"
fishing: "0"
id: "27"
latitude: "0.43082571029663086"
longitude: "51.20949935913086"
motorhome: "1"
pub: "1"
shower: "0"
site_name: "The WoolPack Inn"
swimming: "0"
tent: "1"
toilet: "1"
Edit
Below is the full script with variables/arrays and the map function
let marker = [];
let markerCatBinary = [];
let infoWindow = [];
let lonMarker = [];
let latMarker = [];
let icon = [];
let siteName = [];
let map;
let contents = [];
let id = [];
let motorhome = [];
let caravan = [];
let tent = [];
let elect = [];
let toilet = [];
let shower = [];
let dog = [];
let fishing = [];
let beach = [];
let swimming = [];
let pub = [];
let adult = [];
let len;
function initMap() {
const center = { lat: 52, lng: 0.5 };
// The map, centered at kent
map = new google.maps.Map(document.getElementById("map"), {
zoom: 6,
center: center,
});
$.ajax({
url: "getMapData.php",
dataType: "json",
success: function (data) {
console.log(data);
len = Object.keys(data).length;
for (i = 0; i <= len - 1; i++) {
markerCatBinary[i] += data[i]['motorhome']
markerCatBinary[i] += data[i]['caravan'];
markerCatBinary[i] += data[i]['tent'];
markerCatBinary[i] += data[i]['elect'];
markerCatBinary[i] += data[i]['toilet'];
markerCatBinary[i] += data[i]['shower'];
markerCatBinary[i] += data[i]['dog'];
markerCatBinary[i] += data[i]['fishing'];
markerCatBinary[i] += data[i]['beach'];
markerCatBinary[i] += data[i]['swimming'];
markerCatBinary[i] += data[i]['pub'];
markerCatBinary[i] += data[i]['adult'];
}
console.log("incoming Binary = ", markerCatBinary);
}
});
}
markerCatBinary
? Can you add that code to the question? – Harshana Serasinghe Nov 25 '20 at 10:52undefined
.markerCatBinary[i]
doesn't exist when the loop is entered. The first assignment should bemarkerCatBinary[i] = data[i]['motorhome']
(=
not+=
) instead. – Felix Kling Nov 25 '20 at 11:00