0

I have this piece of JS:

  $.ajax({
    url: '/my-url',
    dataType: 'json',
    success: function(json) {
      for (var i = 0, ien = json.data.length; i < ien; i++) {
        createPanel(json.data[i]);
        var options = json.options;
        console.log(options);
      }
    }
  });

which gets me JSON like this:

{
    "data": [{
        "id": "2",
        "item": {
            "name": "something here...",
            "company": "1"
        },
        "companies": {
            "name": "my super company"
        }
    }],
    "options": {
        "company": [{
            "value": 2,
            "label": "my second name"
        }, {
            "value": 1,
            "label": "my super company"
        }]
    }
}

In console log I can see my json.options which then I need to populate in this piece of code later in same JS file:

  editor.field('company').update(
    // options.company "value", "label" has to be populated here
  );

How do I access my options variable later in my code, please? Thank you!

The solution

Looks like adjusting my code to something like this, does the trick:

  $.ajax({
      url: '/my-url',
      dataType: 'json'
    })
    .done(callback)
    .done(function(json) {
      for (var i = 0, ien = json.data.length; i < ien; i++) {
        createPanel(json.data[i]);
      }
    });

  function callback(response) {
    var options = response.options.company;
    editor.field('company').update(options);
  }
matiss
  • 747
  • 15
  • 36

1 Answers1

-1

You simply need to loop through and get the label and value:

var json = {
    "data": [{
        "id": "2",
        "item": {
            "name": "something here...",
            "company": "1"
        },
        "companies": {
            "name": "my super company"
        }
    }],
    "options": {
        "company": [{
            "value": 2,
            "label": "my second name"
        }, {
            "value": 1,
            "label": "my super company"
        }]
    }
};

$(json.options.company).each(function(k, v){
  console.log('Label: ' + v.label + ' And value is ' + v.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • Why even use jQuery for simply enumerating an array? – Jamiec Aug 13 '17 at 10:23
  • Because OP has tagged JQuery and is using JQuery. Check the code in question. – Milan Chheda Aug 13 '17 at 10:23
  • Presumably that's for the Ajax call, you don't need jQuery to enumerate an array – Jamiec Aug 13 '17 at 10:25
  • Well, he is receiving JSON and he needs values from it which he will be populating somewhere. I wonder, whats wrong here and why the downvote? – Milan Chheda Aug 13 '17 at 10:26
  • @Jamiec The piece of code is taken from bigger JQuery code, so I tagged it. It can probably be vanilla JS as well. – matiss Aug 13 '17 at 10:27
  • Because you're using a clubhammer to crack a nut! – Jamiec Aug 13 '17 at 10:28
  • Alright, so you can provide OP an alternative solution. – Milan Chheda Aug 13 '17 at 10:29
  • @Jamiec I added my solution above. As I'm still learning, could you take a look, please? It's working, however I'd like to know I do it up to some general rules and standards. – matiss Aug 13 '17 at 11:36
  • @matiss yes, that's the right sort of idea. It is asynchronous so you must do things similar to how youve done it. Well done for reading the linked duplicate and trying to understand yourself. – Jamiec Aug 14 '17 at 07:40
  • @Jamiec Thank you for pointing to right direction. I wasn't sure about looping thing as basically my `options` were in JSON already - I just needed to grab them. Thanks again! – matiss Aug 14 '17 at 07:44