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

    )

)
4
  • 1
    Show an example of the data you want to access.... Commented Jul 18, 2019 at 13:14
  • 1
    ^ and the expected output from that data Commented Jul 18, 2019 at 13:16
  • @epascarello already edited the question. thanks! Commented Jul 18, 2019 at 13:20
  • 1
    so you have an array with objects....not strings. So it is not much different, use dot or bracket notation to reference the key in the object. Commented Jul 18, 2019 at 13:23

3 Answers 3

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.

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

Comments

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);
    });

1 Comment

You don't create a new array in javascript as newArray.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.