0

i have a JSON api. simple example is in picture

screen shot with JSON example

all i want to do is add object from JSON to array. like in this example

[{title: 'Marker 1', lat: '40.371268', lang: '49.840576', id: '1', icon 'ico.png'},{ title: 'Marker 2', lat: '40.371268', lang: '49.840576', id: '2', icon 'ico.png'} ]

but when i use this code

 $.getJSON('apiJson.php')
.done(function(data){
       randomMarkers.push(Object.values(data));
   })


console.log(randomMarkers);

i get Array in Array ... cant find how to figure it out.

console.log printscreen

i need an array with object came from outer JSON

sorry for my english.

mr entonee
  • 19
  • 1
  • 1
    `Object.values(data)` returns an array, which you are pushing into an array. Hence you get an array in an array. Just use `randomMarkers = Object.values(data)`. But I suspect that this your attempt to resolve a different issue. See [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ivar Sep 28 '20 at 20:10
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ivar Sep 28 '20 at 20:10
  • randomMarkers = Object.values(data) - does not work ( for your second question - i get response from async call . just cant put it on array may be i need to use for. but cant figure out how ( – mr entonee Sep 28 '20 at 20:14
  • @Ivar just saw your comment. I thought the request will be paginated and thus is trying to push it. – Ayyub Kolsawala Sep 28 '20 at 20:14
  • @mrentonee The link I posted explains exactly why this is causing you trouble and ways you can solve it. (And you might say "_But the `console.log` is showing it, so it must be there_", [but that isn't what it looks like](https://stackoverflow.com/questions/23429203/weird-behavior-with-objects-console-log).) – Ivar Sep 28 '20 at 20:17
  • @AyyubKolsawala In this code `randomMarkers` will never contain any information that is pushed in that callback. Unless crucial details are omitted, this will never work. (I don't think those details are missing. This is a very common misconception.) – Ivar Sep 28 '20 at 20:21

1 Answers1

0

The output of Object.values(data) gives you an array by default. You are trying to push that into another array you declared i.e randomMarkers.

So just replace

randomMarkers.push(Object.values(data));

with

randomMarkers = Object.values(data); and you should be good to go.

Ayyub Kolsawala
  • 809
  • 8
  • 15
  • nothing ( console.log(randomMarkers) - gives undefined – mr entonee Sep 28 '20 at 20:22
  • have you declared the array randomMarkers before using it? `var randomMarkers = [];` – Ayyub Kolsawala Sep 28 '20 at 20:38
  • Yes . I trying 3 types .var randomMarkers; var randomMarkers =[], and var randomMarkers ={}.. in first it gives me undefined.. in second empty array and third empty object. – mr entonee Sep 29 '20 at 04:19
  • @mrentonee Because like I said above, your AJAX request is asynchronous so the `randomMarkers = Object.values(data)` is _always_ called _after_ the `console.log(randomMarkers);`. Again, see the link I mentioned before for more information and possible solutions. (Yes, it's a lot to go through, but it is the only appropriate way to resolve your issue. Also this is an important part of JavaScript so understanding it correctly can also save you a lot of trouble in the future.) – Ivar Sep 29 '20 at 06:49
  • @ivar yes i understand now. I'll try to put all scripts inside getJSON. – mr entonee Sep 30 '20 at 05:37