0

I have jsons:

{id: 10, attr_id: 0, value: "1", extra: null, created_at: "2018-06-05 11:23:34", …}
{id: 11, attr_id: 0, value: "2", extra: null, created_at: "2018-06-05 11:23:50", …}
{id: 12, attr_id: 0, value: "3", extra: null, created_at: "2018-06-05 11:24:06", …}

All these jsons is on each function.

$.each(result, function(index, value) { 
      console.log(value); //I get jsons, which up
      $.each(value, function(i, val) {
         console.log(val.id); //Uncaught TypeError: Cannot read property 'id' of undefined
      });
});

I get error:

Uncaught TypeError: Cannot read property 'id' of undefined.

How I can fix this problem?

UPDATE

I don't need do second each. I just need write value.id. All Working, but I get only first object. How I can get all objects?

Dumitru
  • 2,053
  • 6
  • 20
  • 45
  • if `value` is exactly what you've posted, then it isn't JSON at all – Jaromanda X Jun 05 '18 at 11:26
  • There is no JSON in your question. Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – str Jun 05 '18 at 11:30
  • I want to see how you copied your JSON, check using debugger what is inside your variable. – abdul qayyum Jun 05 '18 at 11:39
  • Json is correct, here 'value' variable is itself your object, So there is no need to use the second loop. – patilprashant6792 Jun 05 '18 at 12:41

4 Answers4

0

Your Json is not in correct format it should be like below:

var result=[{id: 10, attr_id: 0, value: "1", extra: null, created_at: "2018-06-05 11:23:34"},
    {id: 11, attr_id: 0, value: "2", extra: null, created_at: "2018-06-05 11:23:50"},
    {id: 12, attr_id: 0, value: "3", extra: null, created_at: "2018-06-05 11:24:06"}];
    
    $.each(result, function(index, value) {       
      console.log(value.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Rajesh Kumar
  • 2,443
  • 3
  • 28
  • 43
  • I try do JSON.parse(value). I get error: `Unexpected token o in JSON at position 1` – Dumitru Jun 05 '18 at 11:28
  • 1
    Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – str Jun 05 '18 at 11:31
  • So If your json is array or List type then it should be format like var jsonData=[]; [ ] wil contain list of objects. like {id:1,Name:'name1'}, {id:2,Name:'name2'} . so in your case your json data should be like [ {id:1,Name:'name1'}, {id:2,Name:'name2'}] – Rajesh Kumar Jun 05 '18 at 11:34
  • So You want to get all value.id, Right?. have you run code snippet of my answer. You should get all id (10,11,12). If you are not getting all Id that means your json is not a collection of multiple objects or there is only one object in your resulting json. – Rajesh Kumar Jun 05 '18 at 14:19
0

why would you need jQuery to do this, just use vanilla Javascript:

result.forEach(value => {
    console.log(value);
    if ("id" in value) {
      console.log(id + ": " + value[id]);
    }
});

Peace.

JeanSaigne
  • 323
  • 1
  • 10
0

Reason is that the result is of type Array, whereas value is type Object. If the intent is only to find the id in each row, you do not need a inner each loop.

    $.each(result, function(index, value) { 
       console.log(value.id); 
        });

Otherwise, you need to convert the value(object type) to Array type.

0

Thats not a valid json format. You can use jquery each on a json string only after you parse it as JSON which converts it into a js object

($.parseJSON(json_string))

But since your sting isn't a valid json string you cannot parse it as JSON and hence you get that TypeError.

Correct json string would be:

'[{"id": 10, "attr_id": 0, "value": "1", "extra": null, "created_at": "2018-06-05 11:23:34"}, {"id": 10, "attr_id": 0, "value": "1", "extra": null, "created_at": "2018-06-05 11:23:34"}]'

you cannot use literals or identifiers in json, its a string made of strings.

shahburhan
  • 224
  • 1
  • 12