-1

i get this output when i am trying to print the array [object,object]

my array :

 var test=[{
    "football": {
              "icon":"seria A",
              "list":[
                {
                    "url": "adama",
                    "name": "adama"
                },
                {
                    "url": "Kun"
                    "name": "kun"
                }],
 "basketball": {
              "icon":"NBA",
              "list":[
                {
                    "url": "LG",
                    "name": "Lg"
                },
                {
                    "url": "CObe"
                    "name": "COBe"
                    
                }] 
              }
             }]

my code just i am looping into this array return object object :

  $.each(test, function (key){
       console.log(test[key])
     )}

why i get [object object]

  • 1
    Your `test` variable has invalid syntax. – 0stone0 Jun 01 '21 at 11:35
  • If we read past the multiple syntax errors, the reason is that the entries in your array are objects, and it would appear you're using a console that uses the default conversion to string. The default shows `[object Object]` for plain objects. (That's odd for a console, though, most show something more useful.) – T.J. Crowder Jun 01 '21 at 11:36
  • because the result of `test[key]` is not a string and is being coerced in to being a string. Try `console.log(JSON.stringify(test[key]))` – phuzi Jun 01 '21 at 11:37
  • You are missing `,` after `"url": "Kun"` – Carsten Løvbo Andersen Jun 01 '21 at 11:37
  • where is the invalid syntax? –  Jun 01 '21 at 11:37
  • 1
    @IsmailHaji - There are multiple missing `,` and a `]` in the wrong place (at least, there could be more; that was when I stopped trying to fix it). – T.J. Crowder Jun 01 '21 at 11:38
  • Also you start `football` with `{` but where do you close it? – Carsten Løvbo Andersen Jun 01 '21 at 11:38
  • i know i could print it with `console.log(JSON.stringify(test[key]))` but i want just to print normal string @phuzi –  Jun 01 '21 at 11:43
  • 1
    What is a "normal" string? Not sure what you mean. Please explain what output you're expecting – phuzi Jun 01 '21 at 11:47

1 Answers1

1

There are quite some errors in your script. I'll try to address them

  1. Fixed quite some syntax errors, missing comma's, trailing comma's etc.
  2. I guess you don't need an array, an object with 2 keys 'footbal' and 'basketball' seems to be enough
  3. Your closing bracket from the $.each was is wrong order, should be }); instead off )};

Fixing those issues, give us an example like so:

var data = {"football": {"icon":"seria A", "list":[{"url": "adama", "name": "adama"}, {"url": "Kun", "name": "kun"}] }, "basketball": {"icon":"NBA", "list":[{"url": "LG", "name": "Lg"}, {"url": "CObe", "name": "COBe"}] } };

$.each(data, function (key){
   let obj = data[key];
   console.log(key, obj.icon);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
football seria A
basketball NBA
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • thanks for your answer but if i want to get the url inside list –  Jun 01 '21 at 12:05
  • `list` is an array, so you'll [have to loop over the array to get the desired values](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript). – 0stone0 Jun 01 '21 at 12:07
  • $.each(links, function (key, value) { const keys = key; let test = links[key] $.each(links[key], function (key,value) {}) }) is it like that –  Jun 01 '21 at 12:11
  • To show each `list` array, you can do something like [as in this JSFiddle](https://jsfiddle.net/tcvrhpos/). – 0stone0 Jun 01 '21 at 12:14