0

I try to create arrays in arrays and then forward it to JSON.

First problem, when i try to use a lista.length or something, console always return 0. I tried to overpass this problem and create another array, but now I have problem with JSON - always return [] - empty lista array.

var lista = [];
var licz = [];
function ListujBledy(value, where) {
    var checked = document.getElementById(value).checked;
    var desc;
    if (value == "blad-tab") {
        desc = "Nieprzeźroczysta lista graczy.";
    } else if (value == "blad-tab1") {
        desc = "Brak listy graczy na początkowym zrzucie ekranu.";
    } else if (value == "blad-tab2") {
        desc = "Brak listy graczy na końcowym zrzucie ekranu.";
    }
    if (checked == true) {
        if (lista[where] == undefined) {
            var temp = [];
            temp[value] = desc;
            lista[where] = temp;
            licz[where] = 1;
        } else if (licz[where] == 1) {
            var temp = lista[where];
            temp[value] = desc;
            lista[where] = temp;
            licz[where] = 2;
        } else if (licz[where] == 2) {
            var temp = lista[where];
            temp[value] = desc;
            lista[where] = temp;
            licz[where] = 3;
        }
    } else {
        if (licz[where] == 1) {
            delete lista[where];
            licz[where] = 0;
        } else if (licz[where] == 2) {
            delete lista[where][value];
            licz[where] = 1;
        } else if (licz[where] == 3) {
            delete lista[where][value];
            licz[where] = 2;
        }
    }
    console.log(lista.length);
    console.log(lista);
    console.log(JSON.stringify(lista));
    console.log("---------------------------------------------------------");
}

Console log from browser:

Console log from browser

I don't have more ideas, I can't use lista[0], lista[1] etc. everything must be functional. Eveyrthing is taken from variables but everywhere I was looking for information about it, everybody using numbers in key or permanent keys.

Editied version of code: I know that checked could have been better done, so I corrected it here.

https://jsfiddle.net/5vdgLtue/1/

The main problem is that even if I do this https://jsfiddle.net/5vdgLtue/0/ the array returns this element, but the length function says it is 0.

WooQash
  • 13
  • 7

3 Answers3

0

It looks like you might be starting out with javascript. Keep in mind that you haven't actually called the function at any point in your code. Is that the case or are you not sharing the full code you have run?

Jose Jimenez
  • 240
  • 1
  • 8
0

There is only one condition in which the array 'lista' could gain value: if 'check'== true and 'where' == undefined.

In that scenario, you declare the array 'temp' and declare temp[value]= desc. However, if 'value' contains a value different than "blad-tab", "blad-tab1" or "blad-tab2", 'desc' remains empty therefore temp[value] has a name but no value. You are then assigning a named valueless item to lista[where] which would explain why your console displays content but no length. btw, this would be easier if you named your variable something other than 'value' .

Problem is your selector points to the parent element. In jquery you could do this less code but assuming you're not using jQuery. Try something like:

function getDesc(chkboxName) {

  var checkboxes = document.getElementsByName(chkboxName);
 //or use getElementsbyClassName...
  var checkboxesChecked = [];
  // loop over them all
  for (var i=0; i<checkboxes.length; i++) {
     // And stick the checked ones onto an array...
     if (checkboxes[i].checked) {
        checkboxesChecked.push(checkboxes[i]);
     }
   }

 for (var i=0; i<checkboxesChecked.length; i++) {

  if (checkboxesChecked[i].value === "blad-tab") {
          desc = "Nieprzeźroczysta lista graczy.";
      } else if (checkboxesChecked[i].value === "blad-tab1") {
          desc = "Brak listy graczy na początkowym zrzucie ekranu.";
      } else if (checkboxesChecked[i].value === "blad-tab2") {
          desc = "Brak listy graczy na końcowym zrzucie ekranu.";
      }

  }

 return desc;

 }
Vickel
  • 7,879
  • 6
  • 35
  • 56
  • The table fills up, because after calling `console.log(lista);`, the table in the console it displays correctly. It's built correctly (as I want it), but the `lista.length` or JSON functions returns information as if it were empty. – WooQash Dec 02 '18 at 14:05
  • There are no other values for 'value', only 3 options are possible "blad-tab", "blad-tab1" or "blad-tab2", so 'desc' always returns some value. I don't understand that why the console could display array with elements (non empty), but could still return length 0? Simply example (clean project): `var test = []; test["something_key"] = "something_value"; console.log(test); console.log(test.length);` (https://jsfiddle.net/5vdgLtue/). Console return: https://i.imgur.com/XXidGOT.png Arrays have elements but length = 0; Without any strange variables or something. This is a main problem for me. – WooQash Dec 03 '18 at 22:32
  • If you change your fiddle to: var test = []; var temp; test["something_key"] = temp; console.log(test); console.log(test.length); You would find that the console log shows content but no length. You say that 'value' can only return 3 values. Are you sure about that? Can you tell me what console.log(value) and console.log(temp) return in your original question? – Alejandro Ortiz Dec 03 '18 at 23:12
  • Your problem is in: "var checked = document.getElementById(value).checked;". The 'checked' suggests that you are looking through a checkbox or a series of radio buttons. Those checkable elements cannot have a shared id as id's are unique. They would have to be identified with a class name instead. – Alejandro Ortiz Dec 03 '18 at 23:39
  • Yes I'm sure, but I think that is not a main problem at the moment. I know that the values `desc`, `value` and `where` has always filled and there is no situation in which the variable is empty. Ok, look at this: https://jsfiddle.net/5vdgLtue/1/ in my opinion this is a best example for you. Console show everything what we needed. All variables are not empty, array is full, but length is still 0. https://imgur.com/a/g74ajdk – WooQash Dec 04 '18 at 16:36
  • That is indeed the problem. The array is not full. It is empty. You are constructing a name-value pair array. Then you are assigning a NAME to one of the items in the array. The NAME of that item is the dom data for the element that's wrapping your lista array. There is no value to that oddly named item. The name of the empty item is confusing you into thinking that your array contains the values you need but it contains no VALUES. Only a NAME. You're welcome. – Alejandro Ortiz Dec 07 '18 at 17:11
  • So, how do you explain it https://jsfiddle.net/5vdgLtue/0/ why this array is empty? What am I doing wrong? – WooQash Dec 07 '18 at 21:19
0

This should answer most of your questions.

In summary:

In javascript there are 2 types of arrays: standard arrays and associative arrays

[ ] - standard array - 0 based integer indexes only

{ } - associative array - javascript objects where keys can be any strings

What you are doing is using array in an associative manner. Basically, you are adding properties to your array objects, unlike a standard array where you would only assign values by zero-indexed numbers like temp[0]='something', lista[1]='some other thing' etc.

If you want the length of the key set of the array, then you can use Object.keys(lista).length. This should solve your problem.

Community
  • 1
  • 1
Harihar Das
  • 484
  • 3
  • 11