0

I want to read stored values in a array one by one and append it in a html table I can read data from array it fetches all values at one time I want to read values one by one but there is some issue .Please help me.

<script>
    var time="";
    var temp="";
    var current="";
    $(document).ready(function storetime(){

    time=['11:34:4','11:43:4','11:55:4','11:22:4','11:11:4'];
    temp=[33,44,55,66,77];
    current=[22,33,444,55,666];

var tim =[[time,temp,current]];
$.each(tim,function(i,val){
               $('#abc').append(`<tr><td>`+(this)[0]+`</td><td>`+(this)[1]+`</td><td>`+(this)[2]+`</td></tr>`); 
        });


});
</script>


````````output``````````````
Time Stamp  Temperature Current
11:34:4,11:43:4,11:55:4,11:22:4,11:11:4 33,44,55,66,77  22,33,444,55,666 

`````````````````I want output like this``````````

Time Stamp  Temperature Current
11:34:4 22  44
11:34:5 44  44
11:34:6 33  22
11:34:7 11  0
11:34:8 
2
  • 1
    Why don't you use a for loop? Commented Mar 18, 2020 at 17:57
  • array in an array to make the situation more complex, inefficient? Use them separately, make 1 row and then use for loop for all of them. Commented Mar 18, 2020 at 18:03

4 Answers 4

2

You should do like this,

var time="";
var temp="";
var current="";
$(document).ready(function storetime(){

time=['11:34:4','11:43:4','11:55:4','11:22:4','11:11:4'];
temp=[33,44,55,66,77];
current=[22,33,444,55,666];

var tim =[time,temp,current];
$.each(time,function(i,val){
    $('#abc').append(`<tr><td>`+tim[0][i]+`</td><td>`+tim[1][i]+`</td><td>`+tim[2][i]+`</td></tr>`); 
});

As we know all the array we have in here(time, temp, current) has the same length. So, we need to iterate using one of the arrays. Besides its not necessary to make tim a nested array.

Sign up to request clarification or add additional context in comments.

Comments

1

Use the index to refrence the other arrays

var time = [];
var temp = [];
var current = [];
$(document).ready(function storetime() {
  time = ['11:34:4', '11:43:4', '11:55:4', '11:22:4', '11:11:4'];
  temp = [33, 44, 55, 66, 77];
  current = [22, 33, 444, 55, 666];

  $.each(time, function(i, val) {
    $('#abc').append(
      `<tr><td>` +
        val +
        `</td><td>` +
        temp[i] +
        `</td><td>` +
        current[i] +
        `</td></tr>`
    );
  });
});
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="abc">
  <tr>
    <th>Time Stamp</th>
    <th>Temperature</th>
    <th>current</th>
  </tr>
</table>

Comments

0

You have two brackets when initializing tim:

[[time,temp,current]]

and tim[0] is [time,temp,current] which looks like

[['11:34:4','11:43:4','11:55:4','11:22:4','11:11:4'],
 [33,44,55,66,77],
 [22,33,444,55,666]]

And again you have nested arrays, so in your case tim[0][0] would be ['11:34:4','11:43:4','11:55:4','11:22:4','11:11:4'].

You can reduce unnecessary brackets or you can access elements the right way.

Comments

0

    var time="";
    var temp="";
    var current="";
    $(document).ready(function storetime(){

    time=['11:34:4','11:43:4','11:55:4','11:22:4','11:11:4'];
    temp=[33,44,55,66,77];
    current=[22,33,444,55,666];

var tim =[time,temp,current];

var length = tim[0].length;

for(let i=0;i<length;i++){

               $('#abc').append(`<tr><td>`+tim[0][i]+`</td><td>`+tim[1][i]+`</td><td>`+tim[2][i]+`</td></tr>`); 
       
}

});
td{
width:100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='abc'></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.