-1

I have an array stored in a variable called data_array. When alerting, the data is shown as follows:

1 car

2 truck

3 boat

I would like to extract the second column of data based upon the first column.

If col1 = 1 then var1 = car, if col1 = 2 then var2 = truck, if col1 = 3 then var3 = boat.

Basically, I would like to assign the data in the second column to a unique variable based upon the first column. I am utilizing javascript. Any help is appreciated.

For example, I am trying something like this:

function myCallback(data_array){
    alert(data_array);

    var [col1, , var1] = data_array;  
    alert(col1 + " " + var1);
}

However I only have access to the first row and the output is:

1 c

-Alan

...as you can tell, I'm pretty green but I am learning daily, 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); // this produces what I thought was an array, it displays information from a SQL database with 2 columns and 3 rows
  }

  if (this.readyState == 4) {
    myCallback(this.responseText);
  }

  function myCallback(data_array) {

    var [col1, , var1] = data_array;
    alert(col1 + " " + var1); // this is where I cannot figure out how to pull information from subsequent rows

  }
};

xhttp.open("GET", "ajax3.php?", true);
xhttp.send();
Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
genuis
  • 1
  • 2
  • 2
    It looks to me like this is not an array (of any dimension) but simply a string. – CherryDT Apr 19 '22 at 19:57
  • "*When alerting, the data is shown as follows*" - please show us how you create the data, and provide a [mcve] with runnable code where we can see what the data really is. Alerting doesn't help a lot. – Bergi Apr 19 '22 at 20:06
  • If your data is in the format you claim it is, [your code works as expected, as you can see this fiddle:](https://jsfiddle.net/anied/at5m0uqh/1/) https://jsfiddle.net/anied/at5m0uqh/1/. It would appear that you have omitted critical context that will allow the community to assist you with your issue. It might be worth reviewing [ask] and making sure that you have a [mcve] included in your questions; as it currently stands, your issue is not reproducible and thus it will be difficult for anyone to provide you with an answer. Good luck, and happy coding! – Alexander Nied Apr 19 '22 at 20:20
  • the data is being returned from an SQL database into a single variable, I'll post some sample code – genuis Apr 19 '22 at 21:26
  • Providing more code is helpful, but I think what will probably be critical here is posting a sample of what is returned from your data fetch. I believe that [CherryDT's comment is correct](https://stackoverflow.com/questions/71930440/destructuring-an-array-2-columns-by-3-rows-stored-in-a-single-variable-into-3?noredirect=1#comment127104764_71930440), and you are probably _not_ manipulating a true array, but a string. Providing the raw output here would probably be the most useful context for the community to be able to assist you. – Alexander Nied Apr 20 '22 at 13:12

2 Answers2

0

let data_array = [
  [1, "car"],
  [2, "truck"],
  [3, "boat"]
]

let [col1, var1] = data_array[1]

console.log(col1, var1)

I don't know if this the soltion but this is what I got from the question

Nashaat Mohamed
  • 341
  • 2
  • 6
0

I did my best to help you

function myCallback(data_array){
   const [col1 , col2 , col3] = data_array;
   console.log(col1 , col2 , col3);
}

const dataArray = [
  [1 , "col1"],
  [2 , "col2"],
  [3 , "col3"],
];

myCallback(dataArray)

Now if you want to access the second column of each array

console.log(col1[1])

You can also take a look at this document for more information, my friend

davood beheshti
  • 213
  • 1
  • 11