0

Inside an array is a string. I want to loop thru the array and get the specific string inside the array using jquery. Just like in php where I can access the data like

$array_data[] = $data; 
foreach($array_data as $value){}
echo $value[0]['id'];

So i want to do that on jquery. Please Help

 var data = newArray[];
  $.each(data, function () { 

  });

 console.log(data[0]);

The data should look like this

Array
(
[0] => Array
    (
        [id] => 1
        [transaction_id] => 1
        [id_number] => 12102374
        [resource_id] => 110
        [start_date] => 2019-07-25
        [end_date] => 2019-07-27
    )

[1] => Array
    (
        [id] => 3
        [transaction_id] => 3
        [id_number] => 13103132
        [resource_id] => 187
        [start_date] => 2019-07-30
        [end_date] => 2019-08-01

    )

)
Anthony.Jay
  • 57
  • 1
  • 8

3 Answers3

0

You can loop through it like this:

var data = ['String 1', 'String 2'];
  $.each(data, function (i, val) { 
    console.log('Index: ' + i + ' Value: ' + val)
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Also in Javascript, you declare a new array with [] not newArray.

designtocode
  • 2,215
  • 4
  • 21
  • 35
0

You can use the following:

var data = [{"id":"red","category":"young"},{"id":"blue","category":"old"}];
    $.each(data, function(index, val) {
    console.log(val.id);
});

Example taken of this stackoverflow answer.

Kaiser
  • 1,957
  • 1
  • 19
  • 28
0

try

var data = [{"id": 101,"name":"temp name"}];

    data.forEach(function(item) {
        // do something with `item`
    console.log(item.id);
    console.log(item.name);
    });
Galaxy Patel
  • 154
  • 7