0

I would like to not have to declare a lot of different arrays to access different values, like I did in my code.

What should I change in the function structure to have something like an array of arrays, or something else that will allow me to access it like ipOpts[array], example: ipOpts[0], ipOpts[1].

So I will just have to declare it once.


JS Function:
var ipOpts0 = new Array({"label" : "a", "value" : "a"});
var ipOpts1 = new Array({"label" : "a", "value" : "a"});
var ipOpts2 = new Array({"label" : "a", "value" : "a"});
var ipOpts3 = new Array({"label" : "a", "value" : "a"});
var ipOpts4 = new Array({"label" : "a", "value" : "a"});
var ipOpts5 = new Array({"label" : "a", "value" : "a"});
var ipOpts6 = new Array({"label" : "a", "value" : "a"});

function ipOpts(myField,myArray){
    myArray.splice(0,1);
    $.ajax({
      url: './php/selectBox.php?myField='+myField,
      async: false,
      dataType: 'json',
      success: function (json) {
          for(var a=0;a<json.length;a++){
            obj= { "label" : json[a][0], "value" : json[a][1]};
            myArray.push(obj);
          }
        }
    });
    return myArray;
}
Ian
  • 50,146
  • 13
  • 101
  • 111
  • `var myArray = [[{...},{...}],[{...},{...}]]; myArray[1][0]` – Moritz Roessler Apr 03 '13 at 15:08
  • 1
    There is no relation between your declared arrays and the AJAX code you have shown. What is that you need ? – hop Apr 03 '13 at 15:08
  • 1
    Yes, create an array of arrays. What problem do you have with it? Did you use the same the same variable name like your function? – Bergi Apr 03 '13 at 15:10
  • To create an array of arrays. I already understood, thank you guys. –  Apr 03 '13 at 15:11
  • Your ajax function looks like you have a problem with [returning the response from an AJAX call from a function](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call-from-a-function) – Bergi Apr 03 '13 at 15:12

1 Answers1

3

I don't understand why you are creating arrays with 1 object

var ipOpts = [{"label" : "a", "value" : "a"}, 
              {"label" : "b", "value" : "b"}, 
              {"label" : "c", "value" : "c"}]

ipOpts[0]; // {"label" : "a", "value" : "a"}
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • The usage would be ipOpts[0], ipOpts[1], etc? –  Apr 03 '13 at 15:09
  • Yes, for example you can get label of first object like this: ipOpts[0].label this will result "a" – karaxuna Apr 03 '13 at 15:10
  • Good catch on the OPs needs. +1 – Moritz Roessler Apr 03 '13 at 15:11
  • @BernardoLima usually you would access array elements inside a for loop, so that you don't have to explicitly call ipOpts[0] – Jeff Shaver Apr 03 '13 at 15:16
  • @JeffShaver I'm using this function to return different values, ipOpts(myField,myArray), so the function return different values for every different myField value, so I have to store them in different arrays. –  Apr 03 '13 at 15:18
  • @BernardoLima with the code from this answer you aren't storing them in multiple arrays. You are making one array with several objects. – Jeff Shaver Apr 03 '13 at 15:21
  • @JeffShaver you're right, I thought it was an array of arrays, it's really not working. –  Apr 03 '13 at 15:26
  • using `ipOpts[i].label` should work fine to get the values from the objects inside the array. `i` being whatever index you want to use (0,1,2,3,etc...) – Jeff Shaver Apr 03 '13 at 15:31
  • @JeffShaver, I've tried to add ".label" but still not working, look to the usage: "{"label": "Company","name": "company","type": "select","ipOpts": ipOpts('tema', ipOpts[0].label) }" The function should has returned a string. –  Apr 03 '13 at 15:36
  • Is there a way to do something like var "ipOpts = new Array[{"label" : "a", "value" : "a"},{"label" : "a", "value" : "a"},{"label" : "a", "value" : "a"}]; –  Apr 03 '13 at 15:42
  • I've tried var ipOpts = new Array(); for(var i=0;i<7;i++) { ipOpts[i] = {"label" : "a", "value" : "a"} } –  Apr 03 '13 at 15:51