I have a variable named data_array that stores an array. The data appears as:
1 car
2 truck
3 boat
I want to extract the second column of data based on the first column.
If col1 = 1, then var1 = car. If col1 = 2, then var2 = truck. If col1 = 3, then var3 = boat.
Simply put, I want to assign the second column data to distinct variables based on the first column. I'm using JavaScript for this task and any assistance would be great.
Currently, I am attempting the following approach:
function myCallback(data_array){
alert(data_array);
var [col1, , var1] = data_array;
alert(col1 + " " + var1);
}
However, I can only access the first row and the output is:
1 c
-Alan
I am relatively new at this but learning every day. Here is more of my code:
xhttp.onreadystatechange = function() {
var xhttp = new XMLHttpRequest();
if (this.readyState == 4 && this.status == 200) {
data_array = this.responseText;
alert(data_array);
}
if (this.readyState == 4) {
myCallback(this.responseText);
}
function myCallback(data_array) {
var [col1, , var1] = data_array;
alert(col1 + " " + var1);
}
};
xhttp.open("GET", "ajax3.php?", true);
xhttp.send();